Pages

Friday, February 11, 2011

AS3: How to create custom event class with extra parameters

We can create a custom event class with extra properties to pass data with it. The source code is given below.

SOURCE CODE
package 
{
  import flash.events.Event;

  /**
  * @file CEvent.as
  * @author Abhishek Kumar
  */
  public class CEvent extends Event
  {
    public static const CUSTOM_EVENT: String = "custom_event";

    public var data:Object;

    public function CEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
    {
      super(type, bubbles, cancelable);
      this.data = data;
    }

    override public function clone():Event
    {
      return new CEvent(type, data, bubbles, cancelable);
    } 
  }
}
--
Note: Here we can dispatch event as show below:
dispatchEvent(new CEvent(CEvent.CUSTOM_EVENT, objekt));

No comments:

Post a Comment