代码之家  ›  专栏  ›  技术社区  ›  Iain

ActionScript 2事件的最佳实践-有没有办法模拟ActionScript 3样式的事件?

  •  8
  • Iain  · 技术社区  · 16 年前

    我喜欢AS3事件模型-它有助于保持我的代码干净和丢失耦合。当我过去从事AS2项目时,我的代码不是很整洁,类之间的依赖性更强。由于AS2对范围的奇怪处理,我从未真正了解AS2事件系统。

    是否有人成功地在AS2中模拟了AS3事件API,如果没有,侦听和调度事件以及处理范围的最佳实践是什么?

    3 回复  |  直到 16 年前
        1
  •  9
  •   Matt W    16 年前

    实际上,这很容易做到。几节课就能让你走了。第一个是 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 . 首先是非 电影剪辑 版本

    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 { }
    }
    

    下一个 版本

    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!
        }
    }
    

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

        2
  •  2
  •   Jacob Poul Richardt    16 年前

    http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00002325.html

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

        3
  •  0
  •   Mims H. Wright    15 年前

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

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