| 1 |
package |
|---|
| 2 |
{ |
|---|
| 3 |
import flash.events.Event; |
|---|
| 4 |
import flash.events.EventDispatcher; |
|---|
| 5 |
|
|---|
| 6 |
/** |
|---|
| 7 |
* 登録されたアクションを連続して呼び出していくクラス。 |
|---|
| 8 |
* イベントも登録した場合、イベント終了後に次のアクションを実行する |
|---|
| 9 |
* |
|---|
| 10 |
* @param thisObj 関数でthisとして使われるオブジェクト |
|---|
| 11 |
* @param func 関数の参照 |
|---|
| 12 |
* @param params パラメーターを配列で |
|---|
| 13 |
* @param completeEventDispatcher この処理の終了イベントを出すオブジェクト |
|---|
| 14 |
* @param completeEventName この処理の終了イベント名 |
|---|
| 15 |
* |
|---|
| 16 |
* var aq : actionQueue = new ActionQueue(); |
|---|
| 17 |
* aq.addAction( this, myFunc ); |
|---|
| 18 |
* aq.addAction( this, myFunc2, [0,2], this, Event.COMPLETE ); |
|---|
| 19 |
* aq.execute(); |
|---|
| 20 |
*/ |
|---|
| 21 |
public class ActionQueue extends EventDispatcher |
|---|
| 22 |
{ |
|---|
| 23 |
protected var actions : Array |
|---|
| 24 |
protected var _index : Number = 0; |
|---|
| 25 |
public var onComplete : Function; |
|---|
| 26 |
|
|---|
| 27 |
public function ActionQueue():void |
|---|
| 28 |
{ |
|---|
| 29 |
actions = []; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
public function addAction( thisObj : Object, func : Function, params: Array = null , completeEventDispatcher : Object = null, completeEventName : String= null ) : void |
|---|
| 35 |
{ |
|---|
| 36 |
actions.push({ |
|---|
| 37 |
thisObj : thisObj, |
|---|
| 38 |
func : func, |
|---|
| 39 |
completeEventDispatcher : completeEventDispatcher || thisObj, |
|---|
| 40 |
completeEventName : completeEventName, |
|---|
| 41 |
params : params |
|---|
| 42 |
}) |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
//アクションの進行具合を0-100からで表す |
|---|
| 47 |
public function get progress():Number |
|---|
| 48 |
{ |
|---|
| 49 |
return (actions.length>0)? _index / actions.length * 100 : 0; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
//アクションを途中でキャンセルする。 |
|---|
| 54 |
//この場合どうするのがいいのかね? |
|---|
| 55 |
public function cancel() : void |
|---|
| 56 |
{ |
|---|
| 57 |
throw new Error("ActionQueue.cancel is not implemented yet"); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
public function execute() : void |
|---|
| 63 |
{ |
|---|
| 64 |
doNext(); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
public function get index():int |
|---|
| 69 |
{ |
|---|
| 70 |
return _index; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
public function get length():int |
|---|
| 74 |
{ |
|---|
| 75 |
return actions.length; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
protected function doNext() : void |
|---|
| 80 |
{ |
|---|
| 81 |
var act : Object = actions[ _index ]; |
|---|
| 82 |
|
|---|
| 83 |
if( act.completeEventName ){ |
|---|
| 84 |
act.completeEventDispatcher.addEventListener(act.completeEventName, _actionComplete, false, 0, true); |
|---|
| 85 |
act.func.apply( act.thisObj, act.params ); |
|---|
| 86 |
} else { |
|---|
| 87 |
act.func.apply( act.thisObj, act.params ); |
|---|
| 88 |
doNext2(); |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
protected function doNext2() : void |
|---|
| 95 |
{ |
|---|
| 96 |
var act : Object = actions[ _index ]; |
|---|
| 97 |
|
|---|
| 98 |
if( act.completeEventName ) |
|---|
| 99 |
act.completeEventDispatcher.removeEventListener(act.completeEventName, _actionComplete); |
|---|
| 100 |
|
|---|
| 101 |
_index ++; |
|---|
| 102 |
|
|---|
| 103 |
if( _index == actions.length ) |
|---|
| 104 |
{ |
|---|
| 105 |
actions = []; //remove all registerd action for GC |
|---|
| 106 |
if( onComplete != null ) |
|---|
| 107 |
onComplete(); |
|---|
| 108 |
dispatchEvent( new Event(Event.COMPLETE) ); |
|---|
| 109 |
}else{ |
|---|
| 110 |
doNext(); |
|---|
| 111 |
} |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
protected function _actionComplete( e : Event ) : void |
|---|
| 117 |
{ |
|---|
| 118 |
doNext2(); |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|