为ActionScript 2的事件的最佳实践 - 是有没有办法模拟的ActionScript 3式的事件? [英] Best practice for ActionScript 2 events - is there a way to simulate ActionScript 3-style events?

查看:249
本文介绍了为ActionScript 2的事件的最佳实践 - 是有没有办法模拟的ActionScript 3式的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我爱的AS3事件模型 - 它有助于保持我的code干净lossely耦合。在我曾经工作的AS2的项目,我的code就没有那么整齐和类都彼此更加依赖。由于AS2的怪处理的范围,我从来没有真正得到与AS2的事件系统。

I love the AS3 event model - it helps keep my code clean and lossely coupled. When I used to work on AS2 projects, my code was not so neat and classes were more reliant on one another. Due to AS2's strange handling of scope I never really got on with the AS2 event system.

当我还是会偶尔在AS2工作,我的问题是:

As I still occasionally have to work in AS2, my question is:

有没有人成功地模拟AS2的AS3事件的API,如果没有,什么是监听和调度事件和处理范围,最好的做法是什么?

Has anyone managed to simulate the AS3 event API in AS2, and if not, what is the best practice for listening to and dispatching events and handling scope?

推荐答案

它很容易做到这一点,其实。一对夫妇类应该让你去。第一个是一个事件类,如下所示:

Its quite easy to do this, actually. A couple of classes should get you going. The first being an Event class, as follows:

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 + "]";
    }
}

然后,我用两个基类。一个用于常规对象以及对于需要延长影片剪辑的对象。首先,非影片剪辑版本...

Then, I use two other base classes. One for regular objects and on for objects that need to extend MovieClip. First the non MovieClip version...

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

简单的延伸,无论是分派器或DispatcherMC你的对象,你将能够调度事件和监听事件类似于AS3。只是有一些怪癖。例如,当你调用则dispatchEvent()你要传递一个参考对象调度的情况下,通常只是参照对象的属性。

Simply extend your objects with either Dispatcher or DispatcherMC and you will be able to dispatch events and listen for events similarly to AS3. There are just a few quirks. For example, when you call dispatchEvent() you have to pass in a reference to the object dispatching the event, usually just by referring to the object's this property.

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(),以获取事件处理函数的正确范围。例如:

The other quirk is when you want to listen for that event. In AS2 you need to use Delegate.create() to get the correct scope of the event handling function. For example:

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

希望我复制并粘贴这一切都正确地从我的旧文件!希望这个作品送给你。

Hopefully I copied and pasted all this correctly from my old files! Hope this works out for you.

这篇关于为ActionScript 2的事件的最佳实践 - 是有没有办法模拟的ActionScript 3式的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆