関数キューっぽいの

by uwi
♥2 | Line 61 | Modified 2009-12-05 17:55:34 | MIT License
play

ActionScript3 source code

/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9EK2
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    import flash.text.TextField;
    
    public class FunctionQueue extends Sprite {
        public function FunctionQueue() {
            var tf : TextField = new TextField();
            tf.height = 465;
            addChild(tf);
            
            var func1 : Function = function() : void
            {
                tf.appendText("1\n");
            };
            var func2 : Function = function() : void
            {
                tf.appendText("2\n");
            };
            var func3 : Function = function(n : int = 999) : void
            {
                tf.appendText("" + n + "\n");
            };
            
            // [関数名, 実行回数, 引数] どこまでも省略可能
            addEventListener(Event.ENTER_FRAME, makeQueue(
                [
                [func1, 3],
                [null, 30],
                func2,
                [func3, 1, [12345]],
                [func2, 0],
                [],
                [func3, 2],
                ]
                ));
        }
        
        private static function makeQueue(queue : Array) : Function
        {
            return function(e : Event) : void
            {
                var targ : IEventDispatcher = e.target;
                var etype : String = e.type;
                targ.removeEventListener(etype, arguments.callee);
                if(queue.length > 0){
                    var f : * = queue[0];
                    if(typeof(f) == "function"){
                        queue.shift();
                        f();
                    }else if(typeof(f) == "object"){
                        var ct : int = --f[1];
                        if(ct >= 0 && typeof(f[0]) == "function")f[0].apply(null, f[2]);
                        if(ct <= 0)queue.shift();
                    }else{
                        queue.shift();
                    }
                    if(queue.length > 0){
                        targ.addEventListener(etype, makeQueue(queue));
                    }
                }
            }
        }
    }
}