[AS]progression4 LoadCommand系のイベント(開始、終了、ローディング、エラー、中断)のまとめ
progressionのコマンドを使ったときのイベント処理追加(開始、終了、ローディング、エラー、中断)のやり方
何種類か方法があるようなのでまとめ。
1.addEventLinstenerを使った一般的な書き方
public function Test2() {
//コマンドの定義
var loadSound:LoadSound = new LoadSound(new URLRequest("./sound.mp3"));
//イベント処理の追加
loadSound.addEventListener(ExecuteEvent.EXECUTE_START, startHandler);
loadSound.addEventListener(ExecuteEvent.EXECUTE_COMPLETE, completeHandler);
loadSound.addEventListener(ExecuteErrorEvent.EXECUTE_ERROR, errorHandler);
loadSound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
//コマンドの実行
loadSound.execute();
}
private function progressHandler(e:ProgressEvent):void {
trace("e : " + e);
}
private function errorHandler(e:ExecuteErrorEvent):void {
trace("e : " + e);
}
private function completeHandler(e:ExecuteEvent):void {
trace("e : " + e);
}
private function startHandler(e:ExecuteEvent):void {
trace("e : " + e);
}
2.addEventLisnerを使わない書き方
※エラー発生時のイベントについては、onErrorとcatchErrorのどちらでも取れるけど、エラーの内容を参照するには、catchErrorを使っておいた方がいいみたい。
(引数:errorでエラー内容が取れる)
public function Test2() {
//コマンドの定義
var loadSound:LoadSound = new LoadSound(new URLRequest("./sound.mp3"));
//イベント処理の追加
loadSound.onStart = function(){
trace("onStart this : " + this);
};
loadSound.onComplete = function(){
trace("onComplete this : " + this);
};
loadSound.onError = function(){
trace("onError this : " + this);
};
loadSound.catchError = function(target:Object, error:Error){
trace("onError this : " + target ,error);
target.interrupt();
};
loadSound.onProgress = function(){
trace("onProgress this : " + this);
};
//コマンドの実行
loadSound.execute();
}
3.addCommand内とかでコマンドのインスタンスを作れないときのやり方
※エラー発生時のイベントについては、onErrorとcatchErrorのどちらでも取れるけど、エラーの内容を参照するには、catchErrorを使っておいた方がいいみたい。
(引数:errorでエラー内容が取れる)
public function Test2() {
//コマンドの定義
var slist:SerialList = new SerialList();
slist.addCommand(
new LoadSound(new URLRequest("./sound.mp3"), null,
{
//読み込み開始時
onStart:function():void{trace("start : " + this)}
//読み込み完了時
,onComplete:function():void{trace("onComplete : " + this)}
//読み込み中
,onProgress:function():void{ trace("onProgress : "+this); }
//読み込みエラー
,onError:function():void{trace("error : "+ this)}
,catchError:function(target:Object, error:Error):void{trace("error : "+ target,error)}
//読み込み中断時
,onInterrupt:function():void{trace("interrupt : "+ this)}
}
)
);
//コマンドの実行
slist.execute();
}
Tweet