ActionScript Custom Event Tutorial

Learning how to use events can be a tricky process for a new ActionScript developer, but once you get the hang of it, you’ll be surprised how powerful and versatile extending the event class can be. In this post I’m going to show how you can create, dispatch and listen for custom events. For the purposes of this post I am going to assume that we’re all familiar with the basics of object oriented development.

Before we start getting into the code, we should review a few of the basics of the Event class, and how Flash handles them. There are three main elements to any event based system: The event, a listener and a dispatcher. An a event is created in the following manner:

[as3]EventDispatcher(new Event(type));[/as3]

As it’s dispatched, a flash event goes through several phases, climbing up the object tree, then echoing back down. I don’t want to get into the details of these phases, you can read about them in several books on the subject. What I do want to mention is that there are a few things to keep in mind, that are not explicitly mentioned in the documentation:


  1. Flash events dispatched from visual components can be captured by any component in the object tree. That is to say if i capture a event from a text input, I can listen for that event from pretty much any other component.
  2. Non-visual components that dispatch events must have an event listener added directly to an instance of that class. For example, I like to extend the RemoteObject class to get and put data into the database. If i want the remote service to fire off a event that shows that it has received data i need to explicitly do the following:

    var myRemoteService:RemoteObject = new RemoteObject()
    myRemoteService.addEventListener(someEvent, someEventHandler);

Now that we’ve got some basics out of the way, we can look at how to create these custom events. I use custom events in two ways. One, to show specific function events that extend the basics that Flash gives us. The other is to use the event object to carry data from various services and components. For the next example, say we have a RemoteObject that gets get an array of user data from a login request. Once the request goes through, we’re going to use that data to customize the application. The actual data is not important, but the way that we structure our custom event is:


package Events{


public class MyRemoteEvent extends Event{
public static const LOGIN_SUCCESS:String = "success";
public static const LOGIN_FAIL:String = "fail";

private var _userInf:Array;

public function MyRemoteEvent (type:String,

_userInf = userInf;

public function get userInfo():Array{
return _userInf;

override public function clone():Event{
return new CartItemEvent(type,bubbles,cancelable,_userInf);


In the above example, we assign an array in the event class constructor, and store it in the _userInf variable. This can them be accessed through the getter function userInfo(). Whatever you do, don’t forget the clone function. Note that when we return the event in the clone() function, we assign the userInf argument the private variable _userInf. This way the event propigation includes the array we assigned it.


We now can dispatch our custom event in the following way, assuming we have an Array and have imported our new class. We dispatch the event in the following manner:

dispatchEvent(new MyRemoteEvent(MyRemoteEvent.LOGIN_SUCCESS,

That’s it for this post. Next time we’ll look at how to listen for custom events, and call the getter method.

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.