AS3用のCommandライブラリ作ってみた
「任意の関数を実行する」という機能を実装したAS3用のCommandライブラリを作りました。
AS3でのボタンのカスタマイズや、アニメーション、動的な動作変更のたたき台になるんじゃないかと。とりあえず版権フリーです、というか誰かもっと使いやすくして再アップして…
たたき台なのでまだ色々とアレです。
一応、内包してるクラスは以下の通り
Command
関数executeから、登録した任意の関数を実行する。
TimerCommand
関数execute実行後、指定時間たったタイミングで登録した任意の関数を実行する。
EnterFrameCommand
関数execute実行後、指定フレームたったタイミングで任意の関数を実行する。
MacroCommand
関数executeから、登録した複数のCommandサブクラスを一括実行する。
SequenceCommand
関数executeから、登録した複数のCommandサブクラスを一括実行する。MacroCommandが全てのコマンドを一斉に実行するのに対し、こちらは1つ1つ処理が終わる毎に次のCommandを実行する。ファイルロードやアニメーションなど非同期通信向け。
CommandEvent
コマンドの完了時に発行されるcommandCompleteイベントと、複数の関数を実行する場合に1つづつ呼ばれるcommandProgressの2種類。
基本的な使い方は、以下のように使います。
var com:Command = new Command( this, this.test, ["hello ", "world"])
com.execute(); //登録した関数を実行
com.execute("setParameter ", "directory"); //引数を動的に変更
function test( str0:String, str1:String)
{
trace( str0 + str1)
}
こんな感じで、インスタンスcomのexecuteを呼ぶと、それがそのまま関数testに転送されるようになります。 executeに直接引数を渡すこともできます。
MacroCommandクラスを使えば、複数の処理を一括して実行することもできます。
var com0:Command = new Command( this, this.test, ["func1 executed"])
var com1:Command = new Command( this, this.test, ["func2 executed"])
var com2:Command = new Command( this, this.test, ["func3 executed"])
//複数のコマンドをまとめて1つのMacroCommandに登録する
var mCom:MacroCommand = new MacroCommand([com0, com1, com2]);
mCom.execute(); //登録した関数を実行
function test( str:String )
{
trace( str)
}
TimerCommandを使用すれば、特定の処理をmミリ秒ごとにn回実行できます。
//executeを実行すると2秒間隔でのtestを実行を5回繰り返す
var com:TimerCommand = new TimerCommand( this, this.test, ["hello ", "world"], 2000, 5)
com.execute(); //登録した関数を実行
function test( str0:String, str1:String)
{
trace( str0 + str1)
}
SequenceCommandを使えば、MacroCommandのような一括実行に非同期通信を組み込めます。
以下の例は、testを実行し、5秒後にtestを実行し、3秒間隔でのtestを2回実行する
//executeを実行すると2秒間隔でのtestを実行を5回繰り返す
var com:Command = new Command(this, this.test, ["first nomal execute"]);
var tCom0:TimerCommand = new TimerCommand( this, this.test, ["execute with timer"], 5000, 1)
var tCom1:TimerCommand = new TimerCommand( this, this.test, ["execute with timer"], 3000, 2)
var sCom:SequenceCommand = new SequenceCommand([ com, tCom0, tCom1 ]);
sCom.execute();
function test( str:String)
{
trace( str )
}