In my case, the timer I make doesn’t reduce its time whenever a function is called. What code will I change or add in order to reduce the time in my timer?
Timer code:
var count:Number = 1200;
var lessTime:Number = 180;
var totalSecondsLeft:Number = 0;
var timer:Timer = new Timer(1000, count);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timesup);
function countdown(event:TimerEvent) {
totalSecondsLeft = count - timer.currentCount;
this.mainmc.time_txt.text = timeFormat(totalSecondsLeft);
}
function timeFormat(seconds:int):String {
var minutes:int;
var sMinutes:String;
var sSeconds:String;
if(seconds > 59) {
minutes = Math.floor(seconds / 60);
sMinutes = String(minutes);
sSeconds = String(seconds % 60);
} else {
sMinutes = "";
sSeconds = String(seconds);
}
if(sSeconds.length == 1) {
sSeconds = "0" + sSeconds;
}
return sMinutes + ":" + sSeconds;
}
function timesup(e:TimerEvent):void {
gotoAndPlay(14);
}
At this point the timer.start(); is placed on a frame so that the timer starts as it enters the frame.
The
delayproperty onTimeris what you are looking for. In your handler, change the timer’s delay:I assumed by your code sample that you wanted to subtract
lessTimefrom the timer delay on each timer interval. If you want to change the delay to something else, then just adjust the code accordingly.UPDATE
The above code is for decreasing the interval (
delay) between each timer fire. If what you’d like to do instead is decrease the the amount of intervals (repeatCount) it takes for the timer to reachTIMER_COMPLETE, then you want to change therepeatCountproperty onTimer:ANOTHER UPDATE
Keep in mind that when you alter the
repeatCount, it doesn’t affect thecurrentCount. Since you are using a separatecountvariable andtimer.currentCountto calculate the displayed time remaining, it doesn’t look like anything is changing. It actually is though – the timer will complete before the displayed time counts down to zero. To keep your time left display accurate, make sure to subtract the same amount fromcountas you are fromrepeatCount: