One of the problems with Event dispatchers (also called broadcasters in AS2), is they want everyone to know about them! They aren’t content just talking to themselves. They want people to listen, and they’ll pass their event around with fancy bubbles just to disguise it. OK, I kid a bit here of course, but it’s to make a point. EventDispatchers need to have another object listening to them for their event to have any effect outside their own DisplayObject chain. Simply: they need listeners to communicate.

And here's where the fun begins. With centralized event management we’re able to limit the number of listeners and coordinate the speeches of all those busy-body dispatchers.

Finally some code!

In the EventManager we have a method called registerDispatcher. This method is what a broadcaster would call to tell us it's going to be broadcasting events. It's like that HR person you send your resume to if you're one of those busy-body, overly-talkative EventDispatchers.

public function registerDispatcher ( dispatchingObj : IRegisteredDispatcher ) : void
{
        
        /**
* Get The events
* getEvents() is a custom method that must be in any
* class/obj that wants to register as a dispatcher
*/
var eventArray : Array = dispatchingObj.getEvents ( ) ;

/**
* Add the listeners
*/
var len : uint = eventArray.length for ( var i : uint = 0 ; i < len ; i++ )
{
var dispatcherEventName : String = eventArray [ i ] ;
dispatchingObj.addEventListener( dispatcherEventName, recieveEvent )
}

/**
* Save a reference in an array.
*/
dispatchersArray.push ( dispatchingObj );
}


The key to all this is the getEvents() function. It reaches out to that object and asks for a resume of sorts. It's the interviewer that asks the object that wants to register if it's up for the job. When an object wants to register as a dispatcher, it has to provide a list of events it's going to be sending out. It does this by returning an array of strings that represent the names of the events it will dispatch. Let's look at an example:


First, since this type of event management system is best suited for application-wide events, let's look at a custom event our Balloon Popper game will use for letting objects know when a balloon has been popped.

package com.pj_co.balloongame
{
import flash.events.Event;

/**
* Fake tutorial event!
*/
public class BalloonEvent extends Event
{
public static const POP : String = "pop";
public static const SHAKE : String = "shake";
public static const RATTLE : String = "rattle";
public static const ROLL : String = "roll";

public function BalloonEvent( type : String )
{
super( type );
}

public override function clone() : Event
{
return new BalloonEvent( type );
}
}
}



Great! we have an event but who's listening? No one seems to care yet? Well first no one is dispatching this event! Well now that we know what to say, who is going to say it? The balloon class of course. They are the objects that pop! Let's have a look:


package com.pj_co.balloongame
{
import com.pj_co.interfaces.IRegisteredDispatcher;
import com.pj_co.balloongame.BalloonEvent

/**
* Balloons are best popped but we need to tell people about it!
* @author patrick cousins
*/
public class Balloon extends MoiveClip implements IRegisteredDispatcher
{

public function Balloon ( )
{
//constructor stuff here, make a balloon!
}

/////////////////////////////////////////////

/**
* Other Balloon code would go here but we're not going to include all that.
* It's a tutorial after all we're not nuts!
*/
/////////////////////////////////////////////

/**
* Required function for my custom EventManager. This tells the EventManager
* what events to listen for. The EventManager will then add
 
* itself as a listener to this class for these events.
*/
public function getEvents ( ) : Array { var myEventsArray : Array = new Array ( ) ;

myEventsArray [ 0 ] = BalloonEvent.POP
myEventsArray [ 1 ] = BalloonEvent.SHAKE
myEventsArray [ 2 ] = BalloonEvent.RATTLE
myEventsArray [ 3 ] = BalloonEvent.ROLL

return myEventsArray;
}
}
}


Ah, so now we have it! That old friend of ours the getEvents() function. This is what lets everyone know what the baloon will be saying. Except that's the trick. It doesn't actually let everyone know. It just lets the EventManager what to listen for. We do this by returning an array that is just the names of each event we may dispatch later.

So this is still all well and good but we still don't have anyone listening! Read on to the next page to learn about how we can listen in to the events.