- Home
- Tutorials
- Flash
- Intermediate
- ASBroadcaster

Page 2 of 2
Guy Watson
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Guy WatsonASBroadcaster._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);
Spread The Word
Related Articles
1 Response to "ASBroadcaster" 
|
said this on 19 Dec 2007 10:12:02 AM CST
if the obj passed in doe
SBroadcaster.removeListe a si |



Author/Admin)