PDA

View Full Version : Retrieving name of a TimerEvent



way2web
February 27th, 2009, 05:18 AM
Hi,

I'm using multiple timers in a script, but I would like to use the same function with a Switch to define the appropriate action corresponding to the event.target. I've tried e.target.name but it says that name isn't a property of the Timer class...
Any Idea how can I retrieve the names (timer or timer2) within the onTimer function?

Thanks in advance!


var timer:Timer = new Timer(3000);
var timer2:Timer = new Timer(3500);

timer.addEventListener(TimerEvent.TIMER, onTimer);
timer2.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
timer2.start();

var lilStar_mc:LilStar = new LilStar();
var lilStar2_mc:LilStar = new LilStar();
//
function onTimer(e:TimerEvent):void
{
trace(e.target);
lilStar_mc.x= Math.round(Math.random() * stage.stageWidth);
lilStar_mc.y = Math.round(Math.random() * stage.stageHeight);
trace(lilStar_mc.x, lilStar_mc.y);
addChild(lilStar_mc);
lilStar_mc.gotoAndPlay(1);
}

daidai
February 27th, 2009, 06:58 AM
you can do a switch case on event.currentTarget

switch (event.currentTarget)
{
case timer :
trace("timer");
break;
case timer2 :
trace("timer2");
break;
default:
trace("neither");
}
:samuelljackson: