root/as3/Commands/src/commands/SerialCommand.as

リビジョン 609, 2.0 kB (コミッタ: fladdict, コミット時期: 4 年 前)

--

Line 
1 package commands
2 {
3         import flash.events.Event;
4        
5         /**
6          * 「全ての子コマンドを1つづつ実行・終了する」コマンド.
7          *
8          * <p>登録された複数のコマンドを1つづつ順番に実行、終了させていきます。
9          * 最後のコマンドの終了時にEvent.COMPLETEを発行します。</p>
10          *
11          * <p>SerialCommandインスタンスは1回だけの使用を前提とし、再利用は考慮されていません。
12          * 同じ処理を繰り返して行いたい場合には、処理の度に新しいSerialCommandインスタンスを作成してください。</p>
13          *
14          * @example 以下のコードは、hello と表示し、1秒後に world と表示します。
15          * <listing version="3.0">
16          * var ar : Array = [
17          *              new Command("hello"),
18          *              new WaitCommand(1000),
19          *              new Command("world")];
20          *
21          * var sCom : SerialCommand = new SerialCommand( ar );
22          * sCom.addEventListener(Event.COMPLETE, _commandExecuteHandler);
23          *
24          * //ガベージコレクトされないよう、CommandContainerを用いて実行する。
25          * CommandContainer.execute(sCom);
26          * </listing>
27          *
28          * @see commands.CommandContainer
29          */
30         public class SerialCommand extends BatchCommand
31         {
32                 /**
33                  * @param comamndArray ICommandインターフェースを実装したコマンドの配列。
34                  */
35                 public function SerialCommand( commandArray:Array = null)
36                 {
37                         super(commandArray);
38                 }
39                
40                
41                 override public function execute():void
42                 {
43                         doNext();
44                 }
45                
46                
47                 //内部的に次のコマンドを実行
48                 protected function doNext():void
49                 {
50                         var c :ICommand = _commands[ _index ];
51                         c.addEventListener(Event.COMPLETE, doNextCompleteHandler);
52                         c.execute();
53                 }
54                
55                
56                 //子コマンドが終了したときのハンドラ
57                 protected function doNextCompleteHandler( e:Event ):void
58                 {
59                         e.target.removeEventListener(e.type, arguments.callee);
60
61                         _index ++;
62                        
63                         if(_index == _commands.length ){
64                                 this.dispatchComplete();
65                         }else{
66                                 doNext();
67                         }               
68                 }
69         }
70 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。