【问题标题】:Best practice for ActionScript 2 events - is there a way to simulate ActionScript 3-style events?ActionScript 2 事件的最佳实践 - 有没有模拟 ActionScript 3 样式事件的方法?
【发布时间】:2010-10-17 02:00:54
【问题描述】:

我喜欢 AS3 事件模型 - 它有助于保持我的代码整洁和无损耦合。当我过去从事 AS2 项目时,我的代码不是那么整洁,并且类之间更加依赖于彼此。由于 AS2 对范围的奇怪处理,我从未真正了解过 AS2 事件系统。

由于我仍然偶尔需要在 AS2 中工作,所以我的问题是:

有没有人设法在 AS2 中模拟 AS3 事件 API,如果没有,监听和分派事件以及处理范围的最佳做法是什么?

【问题讨论】:

    标签: flash actionscript-3 actionscript-2


    【解决方案1】:

    我猜最好的做法是尽可能使用 EventDispatcher 类。你可以在这里读到它: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00002325.html

    UI 组件也有非常类似于 AS3 的事件调度。

    【讨论】:

      【解决方案2】:

      实际上,这很容易做到。几节课应该能让你继续前进。第一个是Event 类,如下:

      class com.rokkan.events.Event
      {
      
          public static var ACTIVATE:String = "activate";
          public static var ADDED:String = "added";
          public static var CANCEL:String = "cancel";
          public static var CHANGE:String = "change";
          public static var CLOSE:String = "close";
          public static var COMPLETE:String = "complete";
          public static var INIT:String = "init";
      
          // And any other string constants you'd like to use...
      
          public var target;
          public var type:String;
      
          function Event( $target, $type:String )
          {
              target = $target;
              type = $type;
          }
      
          public function toString():String
          {
              return "[Event target=" + target + " type=" + type + "]";
          }
      }
      

      然后,我使用另外两个基类。一个用于常规对象,一个用于需要扩展MovieClip 的对象。首先是非MovieClip 版本...

      import com.rokkan.events.Event;
      import mx.events.EventDispatcher;
      
      class com.rokkan.events.Dispatcher
      {
      
          function Dispatcher()
          {
              EventDispatcher.initialize( this );
          }
      
          private function dispatchEvent( $event:Event ):Void { }
          public function addEventListener( $eventType:String, $handler:Function ):Void { }
          public function removeEventListener( $eventType:String, $handler:Function ):Void { }
      }
      

      接下来是MovieClip 版本...

      import com.rokkan.events.Event;
      import mx.events.EventDispatcher;
      
      class com.rokkan.events.DispatcherMC extends MovieClip
      {
      
          function DispatcherMC()
          {
              EventDispatcher.initialize( this );
          }
      
          private function dispatchEvent( $event:Event ):Void { }
          public function addEventListener( $eventType:String, $handler:Function ):Void { }
          public function removeEventListener( $eventType:String, $handler:Function ):Void { }
      }
      

      只需使用 Dispatcher 或 DispatcherMC 扩展您的对象,您就可以像 AS3 一样分派事件和侦听事件。只有几个怪癖。例如,当您调用 dispatchEvent() 时,您必须传入对调度事件的对象的引用,通常只需引用对象的 this 属性即可。

      import com.rokkan.events.Dispatcher;
      import com.rokkan.events.Event;
      
      class ExampleDispatcher extends Dispatcher
      {
          function ExampleDispatcher()
          {
      
          }
      
          // Call this function somewhere other than within the constructor.
          private function notifyInit():void
          {
                  dispatchEvent( new Event( this, Event.INIT ) );
          }
      }
      

      另一个怪癖是当您想监听该事件时。在 AS2 中,您需要使用 Delegate.create() 来获取事件处理函数的正确范围。例如:

      import com.rokkan.events.Event;
      import mx.utils.Delegate;
      
      class ExampleListener
      {
          private var dispatcher:ExampleDispatcher;
      
          function ExampleDispatcher()
          {
              dispatcher = new ExampleDispatcher();
              dispatcher.addEventListener( Event.INIT, Delegate.create( this, onInit );
          }
      
          private function onInit( event:Event ):void
          {
              // Do stuff!
          }
      }
      

      希望我从旧文件中正确复制并粘贴了所有这些内容!希望这对你有用。

      【讨论】:

      • @Matt W,为什么要使用 $ 符号?
      • @Subhen 这只是一个旧的代码风格约定,我用来表示来自局部变量和实例变量的函数参数。
      【解决方案3】:

      我写了几个类来处理 AS2 中的事件。你可以在这里下载。

      http://dispatchevent.org/mims/as2-eventdispatcher/

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多