This undocumented object is the core of the new Flash MX Event Model. It is used internally to control subscriptions to the predefined objects event notifications, unsubscribe from the predefined objects event notifications and to broadcast event notifications to all subscribed objects for each predefined object. This object can also be used for your own devices and that is when this object really becomes useful…
For help with understanding some of the terms used in this article, please make sure you have read the article entitled "The Flash MX Event Model".
The ASBroadcaster object has four methods:
- ASBroadcaster.initialize //Static Method
- ASBroadcaster.addListener //Passed Down Method
- ASBroadcaster.removeListener //Passed Down Method
- ASBroadcaster.broadcastMessage //Passed Down Method
ASBroadcaster provides the same functionality as the FLEM actionscript library that Branden Hall released in the days of Flash 5. So if you have used FLEM before, then you should have no troubles using this in-built event engine.
ASBroadcaster.initialize(obj);
This method is a static method, meaning it can only be used in the above context, it is called directly as a method of the ASBroadcaster object only. This method is used to add listener functionality to another object. When called, it adds three methods to the passed argument 'obj' and one hidden property:
obj._listeners //hidden property
obj.addListener //method
obj.removeListener //method
obj.broadcastMessage //method
ASBroadcaster.addListener(obj);
This method is added to all objects which are passed as an argument to the ASBroadcaster.initialize method. This method is used to subscribe the given object 'obj' to the object which this method belongs to, event notifications. For example:
Key.addListener(myobject);
Subscribes the object named 'myobject' to the 'Key' objects event notifications. When this method is called, the argument passed 'obj' is added to the _listeners array. This method returns 'true'.
myobject={}; //create a new object
subscribed=Key.addListener(myobject); //subscribe to the Key objects event notifications
trace(subscribed); //outputs 'true'
ASBroadcaster.removeListener(obj);
This method is also added to all objects which are passed as an argument to the ASBroadcaster.initialize method. This method is used to un-subscribe the given object 'obj' from the object which this method belongs to, event notifications. For example:
Key.removeListener(myobject);
Un-subscribes the object named 'myobject' from the 'Key' objects event notifications. When this method is called, the _listeners array is looped through until the object 'obj' is found, when it is found, it is removed from the array.
This method returns 'true' if the object 'obj' is successfully removed from the _listeners array, otherwise it returns 'false' for example if the object named 'obj' was not found in the _listeners array.
myobject={}; //create a new object
Key.addListener(myobject); //subscribe to the Key objects event notifications
removed=Key.removeListener(myobject); //un-subscribe from the Key objects event notifications
trace(subscribed); //outputs 'true'
removed=Key.removeListener(none_existent_object); //un-subscribe an object that isnt already subscribed to the Key event notifications
trace(subscribed); //outputs 'false'
ASBroadcaster.broadcastMessage('theEvent');
This method is also added to all objects which are passed as an argument to the ASBroadcaster.initalize method. This method is used to send an event notification to all the objects which have subscribed to the object which this method belongs to, event notifications. All objects contained inside of the _listeners array in the object this method belongs to, are notified of the event occurring. This method requires one argument 'theEvent', this argument is a string which represents an event handler, found inside of all the subscribing objects, if a property with the name 'theEvent' is found inside of any of the subscribed objects, the function reference contained inside of this property is called.
So for example, whenever the mouse event onMouseMove occurs, the 'Mouse' object would run this line of code:
this.broadcastMessage('onMouseMove');
And all objects contained inside of the Mouse._listeners array would be notified of the onMouseMove event. If any of the objects inside of the Mouse._listeners array contain a property named onMouseMove, the broadcastMessage method attempts to call the contents of this property as a function, therefore the property onMouseMove is an event handler, it needs to contain a function reference.
ASBroadcaster._listeners
This hidden property is an array which contains references to all the objects which are subscribed to the event notification for the object that this property belongs to.
Actionscript Equivalent:
To better explain the actual functionality of this object, how about i show you some code that produces the same functionality as the ASBroadcaster object using flash mx actionscript, the actionscript equivalent:
ASBroadcaster={};
ASBroadcaster.initialize=function(obj)
{
obj.addListener=this.addListener;
obj.removeListener=this.removeListener;
obj.broadcastMessage=this.broadcastMessage;
obj._listeners=[];
}
ASBroadcaster.addListener=function(obj)
{
this.removeListener(obj);
this._listeners.push(obj);
return true;
}
ASBroadcaster.removeListener=function(obj)
{
var a=this._listeners;
var i=this._listeners.length;
while(--i)
{
if(a[i] == obj)
{
a.splice(i,1);
return true;
}
}
return false;
}
ASBroadcaster.broadcastMessage=function(theEvent)
{
var a=this._listeners;
var i=this._listeners.length;
while(--i)
{
a[i][theEvent]();
}
}
If you can read and understand actionscript, you should see what is happening.
Internal ASBroadcaster actionscript:
The actual internal actionscript for this object is not much different:
function AsBroadcaster(){};
o=AsBroadcaster;
o.broadcastMessage=ASnative(101, 12);
o.addListener=function(x)
{
this.removeListener(x);
this._listeners.push(x);
return(true);
}
o.removeListener=function(x)
{
var a=this._listeners;
var i=0;
while(i<a .length)
{
if(a[i]==x)
{
a.splice(i,1);
return(true);
}
i++;
}
return(false);
}
o.initialize=function(o)
{
o.broadcastMessage=ASnative(101,12);
o.addListener=AsBroadcaster.addListener;
o.removeListener=AsBroadcaster.removeListener;
o._listeners=[];
ASSetPropFlag(o,'broadcastMessage, addListener,removeListener,_listeners',13,1);
}
ASSetPropFlags(o,null,3);
Sample Code:
To give an example of the usefulness of the undocumented ASBroadcaster object, we will add an onEnterFrame event notification to the Movieclip object, a very useful event that is currently not available in Flash MX Actionscript, maybe for performance reasons, also note that the Movieclip object doesnt have addListener and removeListener methods in Flash MX Actionscript:
//add 'broadcastMessage', 'addListener', 'removeListener' and '_listeners' to the 'Movieclip' object
ASBroadcaster.initialize(Movieclip);
//create a new movieclip on a far away depth
this.createEmptyMovieClip('__enterframe',-99999);
//broadcast the 'onEnterFrame' event nofitication to all subscribed objects every frame
this.__enterFrame.onEnterFrame=function()
{
Movieclip.broadcastMessage('onEnterFrame');
}
Sample - Example usage:
//create a new object
myobject={};
//define the 'onEnterFrame' event handler
myobject.onEnterFrame=function()
{
trace('onEnterFrame event handler was trigged for 'myobject'');
}
//subscribe to the 'Movieclip' event notifications
Movieclip.addListener(myobject);