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
}
}
}
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;//返回参数对象
}
}
}
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)
}
}
}
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:上面所列出的就是一个简单的不同类间的事件广播与接收的例子(包含参数传递)