Tutorial details:
Written by: Guy Watson , MX 101
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash MX
Topics Covered: How to create your own extensions for the new Event Model.
Assumed knowledge: Variables, Pathing, Scripted Loops.

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.

Continued overleaf...