AsBroadcaster vs. BroadcasterMX vs. EventDispatcher
Much like the way you would subscribe to Mouse events using addListener, we can customize our own broadcasters using AsBroadcaster and BroadcasterMX. In Addition, the EventDispatcher can be used to create custom events for increased granularity.
AsBroadcaster / ASBroadcaster
I must first point out that most all examples on the net of the AsBroadcaster have been for actionscript 1.0. For all other cases, ASBroadcaster has since been renamed AsBroadcaster. Since actionscript 2.0 is case sensitive, it’s very important to point out the lowercase ’s’.
I am really not sure of the differences between AsBroadcaster and BroadcasterMX other than most internal classes ( like onEnterFrameBeacon ) subscribe to BroadcasterMX.
According to a discussion on chattyfig, AsBroadcaster is native and a little faster than EventDispatcher. That same discussion points out that EventDispatcher has more functionality - probably because you can specify alternate event handlers for listeners as well maintaining the ability to subscribe to individual events instead of all events.
var obj = {};
var listener = {};
// initialize
AsBroadcaster.initialize( obj );
// listen
obj.addListener( listener );
// event
listener.onMessage = function()
{
trace(“Hello World”);
obj.removeListener( listener ); // stop listening
}
// broadcast
obj.broadcastMessage(“onMessage”); // trace “Hello World”
obj.broadcastMessage(“onMessage”); // never traces
BroadcasterMX
BroadcasterMX is just like AsBroadcaster although you may have to import the full path mx.transitions.BroadcasterMX.
intrinsic class mx.transitions.BroadcasterMX
{
private var _listeners:Array;
public function addListener(o:Object):Number;
public function broadcastMessage():Void;
static function initialize(o:Object, dontCreateArray:Boolean);
public function removeListener(o:Object):Boolean;
};
*/
// create
var obj = {};
// initialize
mx.transitions.BroadcasterMX.initialize( obj );
// listen
obj.addListener(this);
// event
function onMessage()
{
trace(“Hello World”);
obj.removeListener(this); // stop listening
};
// broadcast
obj.broadcastMessage(“onMessage”); // trace “Hello World”
obj.broadcastMessage(“onMessage”); // never traces
EventDispatcher
Using the EventDispatcher we can customize our event listeners to specify which events to subscribe as well as the method to handle the event, rather than all events broadcasted by an object.
var obj = {};
var listener = {};
// initialize
mx.events.EventDispatcher.initialize ( obj );
// listen
obj.addEventListener( ‘onEvent’, listener );
// event
listener.onEvent = function( eventObj:Object )
{
trace( eventObj.msg );
obj.removeEventListener( ‘onEvent’, listener ); // stop listening
}
// broadcast
var eventObj =
{
target:obj, // origin of event
type:“onEvent” , // event
msg:“Hello World” // additional properties
}
obj.dispatchEvent( eventObj ); // traces “Hello World”
obj.dispatchEvent( eventObj ); // no trace since we are no longer listening to the event
» Permalink » del.icio.us » Digg It!

jgraup said,
December 6, 2006 @ 7:03 pm
Check back:
http://www.gskinner.com/blog/archives/000027.html - gDispatcher
http://www.gskinner.com/blog/archives/2003/09/gdispatcher_bug.html
http://www.shocktime.com/eventdispatcher/ - FStEventDispatcherClass
http://stimpson.flashvacuum.net/mt/archives/2005/03/eventcatcher.html - Eventcatcher
http://stimpson.flashvacuum.net/mt/archives/2005/03/eventcatcher_up.html
jgraup said,
February 22, 2007 @ 8:33 pm
http://www.helpqlodhelp.com/scripts/com.qlod.ExtendedBroadcaster.as
Extend the built-in broadcaster to allow for
manipulating the list of listeners during
the broadcast.
Additionally make the broadcaster a real
class to enable using it like a normal class.
@author Ralf Bokelberg
@credits Shinya Tomikawa, Peter Hall, Andre Michelle, Holger Kohnen
@lastChange 2003/06/05
@history
2003/06/05 published
Ed said,
March 30, 2007 @ 12:20 am
I was just wondering about the difference between AsBroadcaster and EventDispatcher myself. I started writing a class and used AsBroadcaster, then just by chance I see this thing called EventDispatcher and I just ground to a halt trying to figure which to use.
For the life of me I can’t seem to figure out which one is better to use. The only advantage I can see that EventDispatcher has is the ability to subscribe to specific events, which doesn’t seem so hot to me since you could pretty much do the same for AsBroadcaster (i.e. just make callback functions for the eventNames that are of interest for the receiving object).
I’ll reread this in the morning and it might become clearer to me, lol. Great site by the way, keep it up!
jgraup said,
March 30, 2007 @ 11:11 am
I tend to use EventDispatcher all the time because of it’s ability to subscribe/unsubscribe to single events and the way you can proxy those events with a Delegate.
// typical
obj.addEventListener ( ’someEvent’, this );
// scoped and proxied event
obj.addEventListener ( ’someEvent’, mx.utils.Delegate.create ( this, myEventfunction );
The advantage of the later is that you can choose the scope and an alternately named function. The overhead occurs when it passes an object with all the information, not limited to the target and type sent by obj, but any property you want to add.
I use this method in my UIEventMovieClip class:
private function newEvent ( type, eventObj:Object ):Void
{
// check for optional passed prams
var eventObj = ( eventObj != undefined ) ? eventObj : new Object ();
// set event type
eventObj.type = type;
// set sender
eventObj.target = this;
// send event
this.dispatchEvent ( eventObj );
}
and call it by saying:
newEvent ( ’someEvent’, { prop1:true, prop2:false } );
Having a myEventfunction ( eventObj:Object ) { } is kinda cool because you never have to worry about the order of your variables in the function, its just ALWAYS going to send an eventObject as a single pram - so give it a point for consistency and predictability.
By using AsBroadcaster you are getting all events, and those events have to be named the same as you subscribed to. You can choose to not receive events by not making a function of the same name or just unsubscribing to all events.
I would say the pros include, low overhead in the prams passed. It’s probably faster because you spend less time packaging up an object before you send the event, and there is consistency between the event name being broadcast and the method name you use to capture that event.
The drawback is that you receive all events, you don’t know which object sent the event because it doesn’t include a reference to itself. That may be ok when you are subscribing to a Mouse or Stage, since there is only one.
Using the Proxy technique with Event Dispatcher you can easily to the following.
obj1.addEventListener ( ‘onPress’, mx.utils.Delegate.create ( this, obj1_onPress );
obj2.addEventListener ( ‘onPress’, obj2_onPress );
See http://www.justgooddesign.com/blog/jgdelegate.htm for more information on delegates.
I guess when it comes down to it, if you have a class witch TONS of events and TONS of subscribers and you only care about a couple of those events per subscriber, or you want to really section out who you subscribed to and tailor the individual method, the use EventDispatcher.
if you don’t need to tailor the event method and it’s not that big of a deal to receive all events, all of the time, then use AsBroadcaster.
Well, that’s just my opinion.
jgraup said,
March 30, 2007 @ 3:29 pm
On a random note, using the Delegate as a proxy method shown above gives no reference to the subscriber. If you want to use the Delegate in that way, read up on jgDelegate2.as which includes a reference of itself in the function. The only way to unsubscribe is to use scope reference that the delegate has access to when it’s called.
The alternate is to store a variable of the function and pass that as the listener.
jgraup said,
April 2, 2007 @ 7:46 am
How costly is dispatching events using the AS3 event model ?
http://lab.polygonal.de/2007/02/26/how-costly-is-dispatching-events-using-the-as3-event-model/
“By the way, in AS2 the fastest way to dispatch events is using the AsBroadcaster class, and the more listeners you add, the faster it gets compared to direct method invocation.”
Well there ya go!
jgraup said,
June 2, 2007 @ 6:48 pm
Great Explanation by Kirupa
http://www.kirupa.com/developer/actionscript/eventdispatcher.htm
jgraup said,
June 2, 2007 @ 8:01 pm
Muzak made a screencast of using Event Dispatcher and the Delegate as a encapsulation, OOP and best practices example.
The Flash app you seen in the video is part of a larger app that spits out a complete ARP application and actually writes .as files to disk.
http://muzakdeezign.com/flashcoders/create_component/MyButton.html
jgraup said,
June 3, 2007 @ 12:39 am
easy class implementation of eventDispatcher:
class myClass
{
private static var EventDispatcherDependancy = mx.events.EventDispatcher.initialize ( myClass.prototype );
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
public var dispatchQueue:Function;
public function myClass (){}
}