• EventDispatcher in AS3

    ActionScript 3 uses the EventDispatcher (flash.events.EventDispatcher) class for all event handling. This class was available in ActionScript 2, but it existed as an external class in the mx framework. Now, it is built into the player (and in being so, improves performance).

    Whenever you want to create an event handler to be called during a certain event, whether it be every frame (enterFrame event), or at the press of a button (mouseDown event), in AS 3, you will need to use EventDispatcher. This means there are no more onEnterFrame of onPress functions you can define that will automatically handle these events, nor are there any simple addListener methods for generalized event listening. EventDispatcher and addEventListener (and related methods) does it all.

    Methods

    • addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
    • dispatchEvent(event:Event):Boolean
    • hasEventListener(type:String):Boolean
    • removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
    • willTrigger(type:String):Boolean

    Note that addEventListener only takes functions as listeners, not objects. Also remember that class methods are bound to their instances so when used as listeners, ‘this’ in the event call still references the orginal class instance no matter what object dispatched the event.

    Basic Example:

    package {import flash.display.Sprite;
    import flash.events.Event;

    public class MyDispatcher extends Sprite {

    public function MyDispatcher() {
    addEventListener(“customEvent”, handleEvent);
    dispatchEvent(new Event(“customEvent”));
    }

    private function handleEvent(event:Event):void {
    trace(event.type); // “customEvent”
    }
    }
    }

    By extending the EventDispatcher class or any class that inherits from it like Sprite (all DisplayObjects are inherently EventDispatchers), your class gains access to the EventDispatcher methods. Then, other instances (or the class instance itself) can add methods to instances of that class using addEventListener and in turn they can call those events through dispatchEvent.

    If there is some reason your class cannot inherit from the EventDispatcher class (for example, if its already inheriting from another class which does not inherit from EventDispatcher), then you can use the EventDispatcher constructor to intialize your class instance with the methods of EventDispatcher via aggregation (composition). Just make sure you implement the IEventDispatcher interface (flash.events.IEventDispatcher). Ex:

    package {import flash.display.Sprite;
    import flash.events.Event;

    public class MyDispatcher extends Sprite {

    public function MyDispatcher() {
    var dispatcher:CustomDispatcher = new CustomDispatcher();
    dispatcher.addEventListener(“customEvent”, handleEvent);
    dispatcher.dispatchEvent(new Event(“customEvent”));
    }

    private function handleEvent(event:Event):void {
    trace(event.type); // “customEvent”
    }
    }
    }

    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;

    class CustomDispatcher implements IEventDispatcher {

    private var eventDispatcher:EventDispatcher;

    public function CustomDispatcher() {
    eventDispatcher = new EventDispatcher(this);
    }

    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
    eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }
    public function dispatchEvent(event:Event):Boolean {
    return eventDispatcher.dispatchEvent(event);
    }

    public function hasEventListener(type:String):Boolean {
    return eventDispatcher.hasEventListener(type);
    }

    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
    eventDispatcher.removeEventListener(type, listener, useCapture);
    }

    public function willTrigger(type:String):Boolean {
    return eventDispatcher.willTrigger(type);
    }
    }

    The CustomDispatcher helper class above doesn’t inherit from EventDispatcher but uses aggregation to obtain EventDispatcher functionality through an instance of EventDispatcher initialized in the constructor.

    This entry was posted on Friday, October 23rd, 2009 at 7:10 am and is filed under ActionScript, Flash, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    You might also like

    describeType in AS3 that's beautiful.. so much better than typeof.. Though I might also add (if I may), if you want some extremely...
    Events and Event Types in AS3 Event objects used with EventDispatcher in ActionScript 3 are a little less generic than those used with ActionScript...
    Event Handing in AS3 Event handling is the process by which any sort of interactivity is created in ActionScript 3.0. This tutorial...
    Flash Optimization – Activate & Deactivate Events Flash optimization is becoming increasing important & with alternative development tools that developers...
  • 1 Comment

    Take a look at some of the responses we've had to this article.

    1. jacks
      Posted on March 16th

      very nice explanation so basically if we used class which can’t inherent EventDispatcher, we just need to create a class for it then instantiate for it on the main class and then use it right?

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message: