Flash Vista - 主页
本周的热门 Flash 网站
The Two Tales
Site info | 档案文件
主项
主页
新进网站链接
优良评选网站
热门网站
编辑推荐
查找

Flash Tutorials
Flash Resources
FlashVista 新闻
Flash Templates
Flash Intro Templates
Website Templates
Flash Games
Flash Books
FlashVista 票选
本站图


无故定网站连结
English
Mindflood
Site info | 找寻另一个
FlashVista
登录
注册注册
订阅电子报

新建网站链接
编修网站链接
个人喜好网站
建议新分类

打广告
支持
致谢
联落 F

Flash Templates:

More Templates ...

电子报
用户

密码



注册注册
忘记密码?
Partner websites
Free Hit Counter
Free Seo Tools
Free Tutorials
Free Video Tutorials
Forum signatures
Best Free Scripts
Wii Fit
Wii Fit News
Cool Tech Gadgets
Other Resources
推荐
推荐本站给朋友


电子报
状态: 尚未登录

Simplified Chinese English German French Spanish Italian Portuguese Russian Polish Finnish Dutch Swedish Thai Romanian Traditional Chinese Simplified Chinese
查找尚未登录
关键字: 查找:: 高级查找


ASBroadcaster


NEW Flash Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, flash 8 and more.


Printer version



by Guy Watson
www.flashguru.co.uk

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 "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 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);


See the ASBroadcaster entry in the Flashcoders Wiki for further examples.

(注册日期: 02-18-2004, 点阅人次: 0, 评比积分: 3.08, 票数: 12, 评论篇数: 0)
新建至个人喜好 建议本站给朋友

评论篇数: (0)


新建评论
Please note:
We review EVERY comment before it appears on the site, so please dont waste your time by posting spam links :)
No URLs allowed, no HTML please.

If you register or login first, your review will contain your nickname


评分



很棒!
非常好