ActionScript Custom Event Tutorial Part II

This post refers to AS3 Custom Event Tutorial

K, so you’re now extending the event object, having all sorts of fun, bouncing events all up and down your object tree like the destructive monkey that you are. But the question remains: How are you going to capture the events that you are dispatching? The simple answer: the event listener…which may be the most common construct in AS3. The event lister take the following form, where the eventHandler is the function we want to handle the event:


private function eventHandler(event:eventClass):void{

//do something fun here;


If you go back to our last post in this series, you will see that we have extended the basic flash event class into an event type that can carry some data in the form of an array. In this case, we are using it for a hypothetical user login i.e. if the user is authenticated, then our extended event, MyRemoteEvent is dispatched like so:

dispatchEvent(new MyRemoteEvent(MyRemoteEvent.LOGIN_SUCCESS,true,false,infoArray));

To sum up the above we are dispatching an event that is of type LOGIN_SUCCESS, does bubble and is not cancelable. the infoArray argument is any login information that we might be getting from the server. Likewise, if the user login attempt results in a login failure, we dispatch the custom event as so:

dispatchEvent(new MyRemoteEvent(MyRemoteEvent.LOGIN_FAIL,true,false,null));

The important thing to remember here is that the event types you are specifying when you dispatch these  events are  defined as  constants in your event class. You can specify a discrete handler for each possible type of event as in the following:


or you can group the handler functions in sensible groups, and inspect the type property of the event as it comes in as so:


private function genericEventHandler(event:MyRemoteEvent):void{


//do something


//do something else



As you can see in the above, you could add a switch or another if statement to handle various cases that your application might be asked to handle in future.  Just be careful not to make a huge mess in there with nested if statements or 50,000 conditions. And please, please comment your code.

Thats about it for this little tutorial, Happy Coding!

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.