Using EventDispatcher - Page 3
       by Jesse Marangoni aka TheCanadian  |  30 May 2006

In the previous page, we went through a pretty detailed example of how EventDispatcher is used. Let's go through a smaller example on this page with more detailed code explanations.

EventDispatcher and Classes
Using the EventDispatcher in classes isn’t too different but there are a few tricks which can be helpful. And besides, another example never hurt anyone. We’ll dissect a class line for line:

import mx.events.EventDispatcher;
class Interval extends Object {
private static var EventDispatcherDependancy = EventDispatcher.initialize(Interval.prototype);
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
private var __timerInt:Number;
private var __count:Number;
public function Interval(time:Number) {
this.__count = 0;
this.__timerInt = setInterval(this, "increment", time);
}
private function increment():Void {
this.dispatchEvent({type:"interval", num:__count});
this.__count++;
}
}

Code Explanation


import mx.events.EventDispatcher;

Import the EventDispatcher class.


class Interval extends Object {
Define a class called Interval for all of our timing needs.


private static var EventDispatcherDependancy = EventDispatcher.initialize(Interval.prototype);

Here’s where we start off with EventDispatcher in our class. In the previous examples we initialized each broadcaster individually. We could do this here and initialize each instance as a broadcaster in the class constructor. But this is the 21st century – an age of fast cars, television and class prototypes. By giving the EventDispatcher methods to the class prototype, we eliminate the need for each instance to have its own method (which will take up more memory).


public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;

These are the methods that come from the EventDispatcher initialization. We must declare them as members of our class or the compiler will give us an error claiming that there is no such property.


private var __timerInt:Number;
private var __count:Number;

Just some private variables for our class, this has nothing to do with EventDispatcher


public function Interval(time:Number) {

Class constructor.


this.__count = 0;

Start the count of at 0.


this.__timerInt = setInterval(this, "increment", time);

Set the gears in motion, we call the increment method of the class every number of milliseconds as passed to the constructor.


private function increment():Void {

Private method.


this.__count++;

Increment the count property.


this.dispatchEvent({type:"interval", count:__count});

And this is where the magic happens. You’ll see that the object passed to the dispatchEvent method contains the mandatory property type as well as a custom count property which stores the amount of times the event has been dispatched.

And an example of the class in use:

this.intTest = new Interval(1000);
this.objListener = new Object();
this.objListener.interval = function(objEvent:Object) {
trace("Interval dispatched, total intervals: " + objEvent.count + ".\n");
};
this.intTest.addEventListener("interval", this.objListener);

Conclusion
I hope this gives you a better understanding of how EventDispatcher works and how to implement it in your own movies. If you have any questions or suggestions for improvement, feel free to comment on the forums where a lot of people will be happy to help.

Good Luck!

Jesse Marangoni
TheCanadian

 

 

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.