実際、これを行うのは非常に簡単です。いくつかのクラスがあなたを動かすはずです。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!
}
}
うまくいけば、これらすべてを古いファイルから正しくコピーして貼り付けました! これがうまくいくことを願っています。