var c:SerialCommand = new SerialCommand([]); c.pushCommand(null, trace, ["trace1"]); c.pushWaitCommand(100); c.pushCommand(null, trace, ["trace2"]); c.pushWaitCommand(100); c.pushCommand(null, trace, ["trace3"]); c.execute();
Commandクラス
最もシンプルなCommandクラスです。関数execute()を実行することで、インスタンス作成時に指定した任意の処理を実行します。
Commandクラスのコンストラクタでは、以下のパラメータを指定できます。
- thisObj:Object
- 関数のスコープ。通常はnullで省略できる
- function:Function
- 実行したい関数の参照
- params:Array
- 実行時に関数に渡したい引数格納した配列
以下の例は、関数execute()を実行すると、traceを使ってhello worldと出力します。
var command:ICommand = new Command(null, trace, ["hello world"]); command.execute();
またexecuteがコールされたタイミングで、イベントEvent.COMPLETEが発行されます。
WaitCommandクラス
関数executeを実行し、一定時間経過後にEvent.COMPLETEを発行するコマンドです。
- delay:Number 待ち時間のミリ秒表現。
単体ではあまり意味がありませんが、連続的な処理を行う間にインターバルを起きたい場合などに用います。
var command:ICommand = new WaitCommand(3000);
command.addEventListener(Event.COMPLETE, function():void{ trace("executeがコールされてから一定時間経過しました。") });
command.execute();
FrameWaitCommandクラス
WaitCommand?と同様に一定時間処理を待つコマンドですが、時間の代わりにフレーム数を指定できます。
SerialCommandクラス
複数のコマンドを配列として保持し、1つ1つ順番に実行していくコマンドです。
最後のコマンドの処理が終了した時点で、イベントEvent.COMPLETEを発行します。
SerialCommand?の処理は非同期になるので、コマンドの実行中はインスタンスの参照をプロパティ等で保持するように注意してください。
ローカル変数でSericalCommand?を作成、実行した場合、ガベッジコレククタに回収されてしまう可能性があります。
以下の例では、まずhelloと出力し、次に1秒待ち、worldを出力してから、Event.Completeを発行します。
var scommand:SerialCommand = new SerialCommand([]); scommand.push( new Command(null, trace, ["hello"]) ); scommand.push( new WaitCommand(1000) ); scommand.push( new Command(null, trace, ["world"]) ); scommand.execute();
ParallelCommandクラス
CommandContainerクラス
ローカル変数として作成した非同期なコマンドがガベッジコレクションされないように、コマンド終了時まで参照を保持するためのクラスです。
静的関数execute()にコマンドインスタンスを渡すことでコマンドを実行し、EventComplete?を受信して参照を開放します。
CommandContainer.execute( myCommand );

