有时候 , 把Event事件到处写, 确实 不是 什么 好事 , 不利于 调试 和 管理 .
虽然 在 我以前 写的<AS3关于Event>有关于对 Listener 的 Fn的 集中式管理 方法.但是 关于 注册事件的 集中式 管理 好像 没有吧 !!!
先放些代码 -> 仅供参考:
package{ public var AinyEvent:Dispatcher = new Dispatcher(); } class Dispatcher extends EventDispatcher{ private var dict:Dictionary=new Dictionary(); public function listenClassVo(who:Class, callback:Function):void{ this.addEventListener(getQualifiedClassName(who),function (e:Event):void{ callback((e as DataEvent).data as who); }); } public function dispatchClassVo(vo:*, bubbles:Boolean=false,cancelable:Boolean=false):Boolean { return super.dispatchEvent(new DataEvent(getQualifiedClassName(vo), vo, bubbles, cancelable)); } public function unLoadListen(event:String, callback:Function):void{ if(dict.hasOwnProperty(callback)){ this.removeEventListener(event, dict[callback]); delete dict[callback]; } } public function addAinylisten(event:String, callback:Function, once:Boolean=false):void{ var obj:*=this; var fn:Function = function (e:Event):void{ if(once){ obj.removeEventListener(event,fn); } callback(getData(e)); } if(callback is Function){ this.dict[callback]=fn; this.addEventListener(event,fn); } } public function dispatchAinyEvent(event:String, vo:*=null, bubbles:Boolean=false,cancelable:Boolean=false):Boolean { return super.dispatchEvent(new DataEvent(event, vo)); } public function getData(e:Event):*{ return (e as DataEvent)?(e as DataEvent).data:{}; } } class DataEvent extends Event{ public var data:*; public function DataEvent(type:String, vo:*, bubbles:Boolean=false,cancelable:Boolean=false):void { super(type,bubbles,cancelable); this.data = vo; } }