ActionScript 3 Custom Events Explained

­­­­­­­­As we know, ActionScript is an event based language.  Every project you write, you are dealing with events from many different sources like timers, mouse clicks, and enter frames.  But there are times when you want to create some kind of an event that isn’t included in the base Flash events. For example, you want your classes to listen and know about when the state of another class has changed.  To do this, you can create and dispatch custom events.

Today, I will go through the process of creating your own custom event class, naming your custom events, listening for your custom events, and dispatching events for the listeners to actually catch.

First, to create a custom event, create an AS3 class that extends the base Event class (flash.events.Event).  When the class is created, the next step is to mimic the Event’s constructor.  This allows you to create an event of a specific type, but it still has the same behavior of the base Event class when you dispatch the event.  Creating the custom ‘type’ of event is as simple as creating a publicly visible static constant in the class that holds a unique string.

There is a lot you can do with this basic event class, including adding more types of event types or even changing the behavior of the class.  For example, in the code below I added a ‘default’ type of event that can be dispatched without including a type in the declaration.

One more very important piece of information I found when doing some research was the fact that in order to have your custom event fully usable by Flash, you need to override the clone method.  This allows the method to be ‘forwarded’ to another listener; something I do not plan on discussing in this post.  The clone method makes a direct copy of the object and returns it, which is used here and there when messing with some advanced behaviors in AS3.  The code for overriding the clone method is below.

Now that we have a custom event to play with, let’s go over how to actually dispatch this event.  To do this, simply add event listeners for the custom event, and then you can dispatch the event.  I have a small code example below.

Note, however, that this class has the base class of a Movieclip.  One catch when dispatching events is the class must descend from a class called EventDispatcher, which MovieClip does for you.  If you end up having to dispatch an event from a class that isn’t a display object like MovieClip, you simply need to inherit from the EventDispatcher class.

7 Comments.

  1. Lookin’ good here on the interwebz, Jeff. I’m also diggin’ the FeedBurner setup.

    Keep it up. I’m falling behind on all of my blogging lately. Gotta love working all day. :-)

  2. Nice tutorial, Jeff!
    I have a question.
    Dispatching and listening in the same class is no problem, but I have a Main.as which creates many different pages. On these pages, their are objects, like a MyFLVplayer.as which dispatch custom events. But these are not heard by Main. So my workaround is in every PageType.as listen to the custom event, and dispatch that event again, so Main hears it. Now this seems redundant and I was wondering how to do this in a good way?

  3. Hello, Cor!

    First, thanks for visiting! I’m glad that some of my work can come in handy!

    As for your problem.. I believe the solution to this would be to allow your event to bubble up the display list. I had a similar situation like this a couple months back where I first started by “forwarding” the events upward, and I think simply letting it bubble was the quick, easy and elegant solution.

    I might have explained this above or in another one of my posts, but let’s quickly go over it.

    The Event constructor has a three main parameters I use commonly. First, obviously, is the event type. Second is the “bubbles” boolean, and third is the “cancelable” boolean. The important one here bubble. When bubble is set to true, it will allow the event to not only be heard by the direct parent, but the event will “bubble up” through the display list, allowing other objects (such as your Main class, for instance) to hear and react to the event.

    I hope this helps, if not, throw me an email or post back here and I’ll clarify some more!

  4. Hi Jeff,

    Thanks for nice example!

    Request you to please share some example related to bubble event using custom event.

    Regards
    ~R.G

  5. I am actually using 3 custom events to pass data. Do I need to define the public static constant with different values?

  6. Every time I can’t be bothered to write a CustomEvent class for a new project, I google this page. Thanks a lot!
    :wink: