Actionscript 3 countdown timer won't display time in textfield AND nothing happens when timer reaches 0
I created a game in AS3 using a tutorial written for AS2 but I can't get the timer working correctly. The game contains 3 frames, a start screen, game loop, and end screen. the timer starts on frame 2, and counts down (tested this with a trace). When the timer reaches 0 I want to go to frame 3 which is the end game screen but my code isn't working.
var fl_SecondsToCountDown:Number = 30;
var fl_CountDownTimerInstance:Timer = new Timer(1000, fl_SecondsToCountDown);
fl_CountDownTimerInstance.addEventListener(TimerEvent.TIMER, fl_CountDownTimerHandler);
fl_CountDownTimerInstance.start();
theTimer.text = String(fl_SecondsToCountDown);
function fl_CountDownTimerHandler(event:TimerEvent):void
{
if(fl_SecondsToCountDown == 0){;
gotoAndPlay(3);
}else{
trace(fl_SecondsToCountDown + " seconds");
fl_SecondsToCountDown--;
}
}
My second problem is that ny timer (theTimer) doesn't show the time.
EDIT:
var running:Boolean = new Boolean();
running = false;
var time:Number = new Number();
var 开发者_开发技巧fl_SecondsToCountDown:Number = 30;
var fl_CountDownTimerInstance:Timer = new Timer(1000, fl_SecondsToCountDown);
fl_CountDownTimerInstance.addEventListener(TimerEvent.TIMER, fl_CountDownTimerHandler);
fl_CountDownTimerInstance.start();
function fl_CountDownTimerHandler(event:TimerEvent):void
{
time=fl_SecondsToCountDown;
if(time == 0){
running = false;
trace(running);
gotoAndStop(3);
}else{
trace(fl_SecondsToCountDown + " seconds");
fl_SecondsToCountDown--;
theTimer.text = String(fl_SecondsToCountDown);
}
}
Make sure you update theTimer
inside the fl_CountDownTimerHandler
function, otherwise it will only update when it's initialized.
And my guess is that your last countdown check isn't being fired because it'll never run the event again when fl_SecondsToCountDown
is 0 already. Add some more checks with trace
and it will be easier to find out what's going on.
精彩评论