Dictionary + Timer = Delicious

I encountered a problem yesterday in attempting to instantiate many (5-30 ish) timer objects while needing to provide each with a unique parameter.  The ez solution?  Create a dictionary object with whatever arguments you need to pass to your timer.  Then add your timer to the dictionary.  Once your timer is up, simply look up the dictionary definition for your timer event's target.  Here we go:

private var dictionary : Dictionary = new Dictionary();


private function yourFunction(x:Number : void {

var timer:Timer = new Timer(500,1);

dictionary[timer] = x;

timer.start();

timer.addEventListener(TimerEvent.TIMER, callBack);

}


private function callBack(e:Event) : void {

trace("!! my parameter = "dictionary[e.target]);

}


Leave a comment

AS3 and Flash you'll lose your ink over.