8

私は AS3 イベント モデルが大好きです。コードをクリーンで非可逆結合に保つのに役立ちます。以前 AS2 プロジェクトに取り組んでいたとき、私のコードはあまりきれいではなく、クラスは相互に依存していました。AS2 のスコープの扱い方がおかしいため、AS2 イベント システムを実際に使用することはできませんでした。

私はまだ時々 AS2 で作業しなければならないので、私の質問は次のとおりです。

AS2 で AS3 イベント API をシミュレートできた人はいますか?そうでない場合、イベントをリッスンしてディスパッチし、スコープを処理するためのベストプラクティスは何ですか?

4

3 に答える 3

9

実際、これを行うのは非常に簡単です。いくつかのクラスがあなたを動かすはずです。1 つ目は、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 + "]";
    }
}

次に、他の 2 つの基本クラスを使用します。1 つは通常のオブジェクト用、もう 1 つは拡張が必要な​​オブジェクト用です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 ) );
    }
}

もう 1 つの癖は、そのイベントをリッスンする場合です。Delegate.create()AS2では、イベント処理関数の正しいスコープを取得するためにを使用する必要があります。例えば:

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

うまくいけば、これらすべてを古いファイルから正しくコピーして貼り付けました! これがうまくいくことを願っています。

于 2009-04-02T14:33:35.403 に答える
2

ベストプラクティスは、可能な限り EventDispatcher クラスを使用することだと思います。ここでそれについて読むことができます: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00002325.html

UI コンポーネントには、非常に AS3 に似たイベント ディスパッチもあります。

于 2009-04-02T10:30:06.940 に答える
0

AS2 でイベントを処理するためのクラスをいくつか作成しました。ここからダウンロードできます。

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

于 2009-11-12T08:17:06.143 に答える