Skip navigation

Every time when I consider using custom events I have to scour the web for it. So I am writing my personal notes for it.

Here is a good link about forming your custom event:

http://www.charglerode.com/blog/?p=51

Robert Penner also posts his observations on event handling from the Flash community.

One important aspect is the requirement of overriding the clone method (via Keith Peters.) Ben Clinkinbeard quotes on Robert’s post : “clone() is only called if an event is manually re-dispatched by passing it to dispatchEvent() again. So if a parent catches it and then does dispatchEvent( childEvent ), clone() will be used and required. Bubbling alone does not call clone(), but overriding it is good practice.”

My observation is that I have been getting errors in Flash Builder regarding to the order of parameters. So by adding new arguments after all the default arguments would give me errors. This was resolved when I added my new arguments after the “type” argument. So here is a code snippet.

[cc lang=”actionscript3″ tab_size=”2″ lines=”40″ width=”575″ line_numbers=”true”]

package myFilePathToEventClass
{

import flash.events.Event;

public class MyCustomEvent extends Event
{
//Add Custom Event ids here
static const SOMEEVENT:String = “someEvent”;

//Add public properties here (whatever data type) … will be available in the event handler
public param1:String;
public param2:Object;

public function MyCustomEvent(type:String,
param1:String,
param2:Object,
bubbles:Boolean = false,
cancelable:Boolean = true )
{
super(type, bubbles, cancelable);
this.param1 = param1;
this.param2 = param2;
}

override public function clone():Event {
return new MyCustomEvent(type, param1, param2, bubbles, cancelable);
}

override public toString():String {
return formatToString(“MyCustomEvent”, “type”, “param1”, “param2”, “bubbles”, “cancelable”);
}

}

}

[/cc]

3 Comments

  1. Found a bug in the code in line number 27.

    It requires a “new” key word.

    Without it causes this error:

    Incorrect number of arguments. Expected no more than 1.

  2. Hey David, you should not specify :void on your constructor. Also, if you only have only on event type on your class, you can just call super(SOMEEVENT).
    You also have a typo in the argument list, there shouldn’t be a semicolon after the last argument. I would be surprised if this code compiles at all.


Leave a Reply

Your email address will not be published. Required fields are marked *