AS3中不同类间的广播与接收简单例子
?1.文档类:DispatchEventDemo.as
package?{
import?flash.display.Sprite;
public?class?DispatchEventDemo?extends?Sprite
{
??public?function?DispatchEventDemo()
??{
???new?A
???new?B
???
??}
}
}2.中间介质类,连系不同的类:DT.as
package?{
import?flash.events.EventDispatcher;
public?class?DT?{
??private?static??var?_dispatcher:EventDispatcher;
??public?static?function?get?dispatcher():EventDispatcher{
???_dispatcher=_dispatcher==null?new?EventDispatcher:_dispatcher
???return?_dispatcher
???}
}
}3.扩展事件类,方便进行事件中的参数传递:EventX.as
package?{
import?flash.events.Event
public?class?EventX?extends?Event?{
??public?static??const?RECEIVE_ARG:String="receive_arg";
??private?var?_arg:Object
??public?function?EventX(type:String,arg:Object,bubbles:Boolean=false,cancelable:Boolean=false)?{
???super(type,bubbles,cancelable);
???_arg=arg;
??}
??public?function?get?arg():Object?{
???return?_arg;//返回参数对象
??}
}
}4.包含事件接收的类:A.as
package?{
import?flash.events.*
public?class?A?{
??public?function?A()?{
???var?dispatcher:EventDispatcher=DT.dispatcher;
???dispatcher.addEventListener(EventX.RECEIVE_ARG,onTrace);
??}
??private?function?onTrace(evt:EventX):void{
???trace(evt);
???trace(evt.arg.a)
???trace(evt.arg.b)
??}
}
}5.包含事件发送的类:B.as
package?{
public?class?B?{
??import?flash.events.*
??public?function?B()?{
???var?dispatcher:EventDispatcher=DT.dispatcher;
???dispatcher.dispatchEvent(new?EventX(EventX.RECEIVE_ARG,{a:"arg:a",b:"arg:b"}));
??}
}
}//PS:上面所列出的就是一个简单的不同类间的事件广播与接收的例子(包含参数传递)
附件:
?
DispatchEventDemo.rar
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/1965.html