| Register
Saturday, May 17, 2008   

Manage Communications with EventDispatcher

Created By  Satori Canton, at  1/27/2006 - 1 comments.

Click to view this author's website.

In my last article, I wrote about the Flash Delegate, a very useful class that is available as part of the core v2 Flash Components. Another useful class that you'll find among v2 components (and often used with Delegates) is the EventDispatcher.

The EventDispatcher is a class that will mix in event handling capabilities into any other class that you initialize with the EventDispatcher. This will define the methods "addEventListener", "removeEventListener", "dispatchEvent", and "dispatchQueue". Once these methods are defined, your custom class will be able to handle events just like any other prebuilt component.

For example, the following code defines a custom button class that extends a MovieClip and uses EventDistpatcher to initialize its event handling capabilities:

Once the EventDispatcher initializes the class, we can broadcast any event we want by calling the dispatchEvent method. This method takes in one parameter, an object literal that defines the type of event (in this case "click") and a target reference back to the origin of the event. You could also add additional information about the event as additional properties of the object.

To use the CustomButton class, we can create a MovieClip with its linkage identifier and AS 2.0 class as "CustomButton". With an instance of the button on the stage that has an instance name of "myButton", we can use the following code to subscribe to the "click" event of the button and trace out the target to test that our EventDispatcher is working correctly.

It's a quick, cheap and easy way to standardize event handling across all of your classes. You'll never have to deal with _root or this._parent issues again. Combine use of the EventDispatcher with the Delegate and you'll find that your classes will become well structured and rock-solid.

Here are the sample EventDispatcher files from this article including an example of how to use EventDispatcher with a Delegate instead of a direct reference to a method.

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Cliburn Solano  Replied:
    ( 4/18/2007 At 3:31 AM)

    Hello Satori!

    I have a problem here. I created a class named modelListeners for the listeners. Now this class is being extended by another class called modelCircle class. This modelCircle class is being used by a movieclip named "circle" using a linkage identifier. Now movieclip "circle" is inside another movieclip named "container". The movieclip "container" loads a class named Application. Now my problem is this, when I try placing event listeners in the timeline, the program works, when I place it inside the application class, nothing happens...

    Here's the code:

    //Application.as
    class Application extends MovieClip{

    var circle:MovieClip;

    public function Application (Void) {
    trace("Application init");
    this.init();
    }

    public function init(Void):Void {
    circle.addEventListener("pindot_a", this.clicky);
    }

    function clicky () {
    trace("Mabuhay ang Pilipinas!!");
    }

    }

    //modelCircle.as

    import models.modelListeners;

    class models.modelCircle extends modelListeners {

    public function modelCircle(Void) {
    trace("modelCircle init");
    }

    private function onRelease():Void {
    this.dispatchEvent({type:"pindot_a", target:this});
    }

    }

    // modelListeners.as


    class models.modelListeners {

    public function modelListeners() {
    mx.events.EventDispatcher.initialize(this);
    }

    public function dispatchEvent() {};
    public function addEventListener() {};
    public function removeEventListener() {};
    public function dispatchQueue() {};

    }

    Now the movie hierarchy is like this:

    _root.container.circle

    Container loads Application thru linkage identifier
    Circle loads modelCircle thru linkage identifier

    The code works though when I use the _root timeline with a code like this:

    this.clickHandler = function(o:Object):Void {
    trace("Mabuhay ang Pilipinas!!");
    }
    _root.container.circle.addEventListener("pindot_a", this.clickHandler);

    Mabuhay!!

    Cliburn M. Solano

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet