这次先把ActionScript 3.0 事件机制总结一下先吧,这里只说一下自定义类发送事件的3中方式,因为在设计模式中是比较常用的。例如MVC。
关于自定义事件,主要由于两个类:事件类(Event),事件发送类(EventDispatcher)。我们讨论的自定义发送事件,就是如何使对象能够发送事件。
方式一:继承EventDispatcher类
package { import flash.display.Sprite; import flash.events.Event; import flash.events.EventDispatcher; /** * ... * @author 橡树小屋 */ public class estends extends Sprite { function estends() { //第一种 var dispatcher:extendsDispatcher = new extendsDispatcher(); dispatcher.addEventListener(extendsDispatcher.ACTION, actionH); dispatcher.doSome(); //第二种 var dispatcher1:EventDispatcher = new EventDispatcher(); dispatcher1.addEventListener("secDispatcher", actionH); dispatcher1.dispatchEvent(new Event("secDispatcher")); //或者 dispatcher.addEventListener("thurdDispatcher", actionH); dispatcher.dispatchEvent(new Event("thurdDispatcher")); } private function actionH(e:Event):void { trace(e); } } } import flash.events.Event; import flash.events.EventDispatcher; class extendsDispatcher extends EventDispatcher { public static var ACTION:String = "action"; public function doSome():void { dispatchEvent(new Event(extendsDispatcher.ACTION)); } }
只要继承EventDispatcher类,就可以直接用EventDispatcher类的方法发送事件,方便简单。
方式二:复合EventDispatcher对象
package { /** * ... * @author 橡树小屋 */ import flash.display.Sprite; import flash.events.Event; public class compont extends Sprite { public function compont() { var dis:compont1 = new compont1(); dis.getEventDispatch().addEventListener(compont1.ACTION, show); dis.doSome(); } private function show(e:Event):void { trace(e); } } } import flash.events.Event; import flash.events.EventDispatcher; class compont1 { private var _dispatcher:EventDispatcher; public static var ACTION:String = "action"; public function compont1() { _dispatcher = new EventDispatcher(); } public function getEventDispatch():EventDispatcher { return _dispatcher; } public function doSome():void { _dispatcher.dispatchEvent(new Event(compont1.ACTION)); } }
所谓的复合,其实就是用EventDispatcher发送事件而已,因为EventDispatcher本身就是个事件发送类,一般情况下,用EventDispatcher发送事件反而会更简单。
方式三:实现IEventDispatcher接口
package { import flash.display.Sprite; import flash.events.Event; /** * ... * @author 橡树小屋 */ public class implement extends Sprite { public function implement() { var impObj:implete = new implete(); impObj.addEventListener(implete.ACTION, actionH); impObj.dispatchEvent(new Event(implete.ACTION)); } private function actionH(e:Event):void { trace(e); } } } import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; class implete implements IEventDispatcher { public var _dispatcher:EventDispatcher; public static const ACTION:String = "action"; public function implete() { _dispatcher = new EventDispatcher(); } //实现5个方法 public function addEventListener(type:String,listener:Function,useCapture:Boolean=false,priority:int=0,useWeakReference:Boolean=true):void { _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } public function dispatchEvent(evt:Event):Boolean { return _dispatcher.dispatchEvent(evt); } public function hasEventListener(type:String):Boolean { return _dispatcher.hasEventListener(type); } public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { _dispatcher.removeEventListener(type,listener,useCapture); } public function willTrigger(type:String):Boolean { return _dispatcher.willTrigger(type); } }
当类本身要继承其他类,无法继承EventDispatcher类的时候,同时又希望这个类能够成为一个单纯的事件发送类,并且可以自己添加某些方法在里面,这时候用继承IEventDispatcher接口就比较合适了,不过过程比较麻烦写,要实现IEventDispatcher接口的5个方法。
参考资料:
《flash ActionScript 3.0 殿堂之路》