views:

22

answers:

2

I want to initial all states such as c4.currentState='down' so that I could call dictionary key to execute the function, is it possible?

private var keyMap:Dictionary = new Dictionary();
private var c4v1:Object = new Object();
private var c4v0:Object = new Object();


private function initial_keyEvent():void {
    keyMap[c4v1] = "c4.currentState='down'";

}

private function call_keys():void {
    keyMap[c4v1];
}
+1  A: 

Try a function as the value of the dictionary. Something like this:

private var keyMap:Dictionary = new Dictionary();
private var c4v1:Object = new Object();
private var c4v0:Object = new Object();

public function changeC4State():void{
  c4.currentState='down';
}

private function initial_keyEvent():void {
    keyMap[c4v1] = changeC4State;
}

private function call_keys():void {
    var myFunc : Function = keyMap[c4v1];
    myFunc();
}

Take a look at the ArrayCollection's FilterFuntion property or a list class LabelFunction for some more info about this.

www.Flextras.com
A: 

Currying will help you ! Also, have a look at the Command pattern.

package {
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.utils.Dictionary;
    import flash.display.Sprite;

    public class Main extends Sprite {
        private var dictionary : Dictionary;

        public function Main() {            
            dictionary=new Dictionary();

            var foo1:Foo=new Foo("John");
            var foo2:Foo=new Foo("Jane");

            dictionary[0]=curry(foo1,"Hello world");
            dictionary[1]=curry(foo1,"My name is John");
            dictionary[2]=curry(foo2,"Hello King Kong");
            dictionary[3]=curry(foo2,"My name is Jane");

            var timer:Timer=new Timer(1000,1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
            timer.start();

            function curry(foo:Foo,message:String) : Function {
                var f : Function = function():void {
                    foo.traceIt(message);
                };
                return f;
            }
        }

        private function timerComplete(e : TimerEvent) : void {
            Timer(e.target).removeEventListener(e.type, arguments.callee);
            dictionary[0]();//John trace : Hello world
            dictionary[1]();//John trace : My name is John  
            dictionary[2]();//Jane trace : Hello King Kong  
            dictionary[3]();//Jane trace : My name is Jane

        }
    }
}

class Foo {
    private var name : String;

    public function Foo(name:String) {
        this.name = name;
    }

    public function traceIt(message:String):void{
        trace(name,"trace :",message);
    }


}
OXMO456
Can explain more how will your code benefits vs Switch statement?
my solution is not related to a switch statement... did i miss something...?
OXMO456