ActionScript 3.0 事件机制小结
这次先把ActionScript 3.0 事件机制总结一下先吧,这里只说一下自定义类发送事件的3中方式,因为在设计模式中是比较常用的。例如MVC。
关于自定义事件,主要由于两个类:事件类(Event),事件发送类(EventDispatcher)。我们讨论的自定义发送事件,就是如何使对象能够发送事件。
方式一:继承EventDispatcher类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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 殿堂之路》
分类:
As3.0实验室
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架