AS 发送事件

转自  :   http://blog.csdn.net/aisajiajiao/article/details/6960475



在AS3中实现自定义事件发送者有多种方法,这几天第四次看黑羽的殿堂之路,顺势就写下了,其本质只有一个EventDispatcher。


AS3是面向对象的语言,面向对象思想解析如下图:


所以在这里我们也有多种方法来实现自定义事件发送类。

这里有三种:

1.通过继承来实现;

2.通过复合来实现;

3.通过实现接口来实现。

在AS3事件中有3个要素,如下图所示,我们现在要实现的是自定义一个EventDispatcher。


第一种方法:继承EventDispatcher来实现

  1. package  
  2. {  
  3.     import flash.display.Sprite;  
  4.     import flash.events.Event;  
  5.     import flash.events.EventDispatcher;  
  6.       
  7.     /**  
  8.      * ...  
  9.      * @author aisajiajiao  
  10.      * 使用继承来实现  
  11.      */  
  12.       
  13.     public class CustomEventDispatcherDemo extends Sprite  
  14.     {  
  15.       
  16.         public function CustomEventDispatcherDemo()  
  17.         {  
  18.             var dispatcher:SampleEventDispatcher = new SampleEventDispatcher();  
  19.             //不推荐直接使用字符串表示事件类型,这里只是作为演示  
  20.             dispatcher.addEventListener("Custom",actionListener);  
  21.             dispatcher.dispatchEvent(new Event("Custom"));  
  22.         }  
  23.           
  24.         private function actionListener(e:Event):void  
  25.         {  
  26.             trace("自定义事件发送类" + e);  
  27.         }  
  28.     }  
  29. }  
  30.   
  31. import flash.events.Event;  
  32. import flash.events.EventDispatcher;  
  33.   
  34. //继承  
  35. class SampleEventDispatcher extends EventDispatcher  
  36. {  
  37.     //这里可以写一些自己需要的代码  
  38. }  

第二种方法:复合来实现

  1. package  
  2. {  
  3.     import flash.display.Sprite;  
  4.     import flash.events.Event;  
  5.     import flash.events.EventDispatcher;  
  6.       
  7.     /**  
  8.      * ...  
  9.      * @author aisajiajiao  
  10.      * 使用复合来实现  
  11.      */  
  12.       
  13.     public class CustomEventDispatcherDemo2 extends Sprite  
  14.     {  
  15.       
  16.         public function CustomEventDispatcherDemo2()  
  17.         {  
  18.             var dispatcher:SampleEventDispatcher = new SampleEventDispatcher();  
  19.               
  20.             //这里我们可以看出使用复合之后,该类就不能再看做EventDispatcher对象了  
  21.             dispatcher.getEventDispatcher().addEventListener("Custom",actionListener);  
  22.             dispatcher.getEventDispatcher().dispatchEvent(new Event("Custom"));  
  23.         }  
  24.           
  25.         private function actionListener(e:Event):void  
  26.         {  
  27.             trace("自定义事件发送类" + e);  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. import flash.events.Event;  
  33. import flash.events.EventDispatcher;  
  34.   
  35. class SampleEventDispatcher  
  36. {  
  37.     private var _dispatcher:EventDispatcher;  
  38.       
  39.     public function SampleEventDispatcher()  
  40.     {  
  41.         _dispatcher = new EventDispatcher();  
  42.     }  
  43.       
  44.     //get方法  
  45.     public function getEventDispatcher():EventDispatcher  
  46.     {  
  47.         return _dispatcher;  
  48.     }  
  49. }  

第三种方法,实现IEventDispatcher接口。

  1. package  
  2. {  
  3.     import flash.display.Sprite;  
  4.     import flash.events.Event;  
  5.     import flash.events.EventDispatcher;  
  6.       
  7.     /**  
  8.      * ...  
  9.      * @author aisajiajiao  
  10.      * 使用IEventDispatcher接口  
  11.      */  
  12.       
  13.     public class CustomEventDispatcherDemo3 extends Sprite  
  14.     {  
  15.       
  16.         public function CustomEventDispatcherDemo3()  
  17.         {  
  18.             var dispatcher:SampleEventDispatcher = new SampleEventDispatcher();  
  19.               
  20.             //不推荐直接使用字符串表示事件类型,这里只是作为演示  
  21.             dispatcher.addEventListener("Custom",actionListener);  
  22.             dispatcher.dispatchEvent(new Event("Custom"));  
  23.         }  
  24.           
  25.         private function actionListener(e:Event):void  
  26.         {  
  27.             trace("自定义事件发送类" + e);  
  28.         }  
  29.     }  
  30. }  
  31.   
  32. import flash.events.Event;  
  33. import flash.events.EventDispatcher;  
  34. import flash.events.IEventDispatcher;  
  35.   
  36. //这样灵活性非常大,可以在这里实现你想要的各种功能  
  37. class SampleEventDispatcher implements IEventDispatcher  
  38. {  
  39.     public var _dispatcher:EventDispatcher;  
  40.       
  41.     public function SampleEventDispatcher()  
  42.     {  
  43.         _dispatcher = new EventDispatcher();  
  44.     }  
  45.       
  46.     public function addEventListener(type:String,listener:Function,  
  47.                     useCapture:Boolean = false,priority:int = 0,useWeakReference:Boolean = false):void  
  48.     {  
  49.         _dispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);  
  50.     }  
  51.       
  52.     public function dispatchEvent(event:Event):Boolean  
  53.     {  
  54.         return _dispatcher.dispatchEvent(event);  
  55.     }  
  56.       
  57.     public function hasEventListener(type:String):Boolean  
  58.     {  
  59.         return _dispatcher.hasEventListener(type);  
  60.     }  
  61.       
  62.     //注意这里的参数个数是与addEventListener数目不同的,仅有3个  
  63.     public function removeEventListener(type:String,listener:Function,useCapture:Boolean = false):void  
  64.     {  
  65.         _dispatcher.removeEventListener(type,listener,useCapture);  
  66.     }  
  67.       
  68.     public function willTrigger(type:String):Boolean  
  69.     {  
  70.         return _dispatcher.willTrigger(type);  
  71.     }  
  72. }  

所有自定义事件最终的输出结果如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值