I have a Timer that I am trying to get to countdown instead of up. I worked with this code years ago and can't seem to figure out how to make it count down from 5 minutes.
I've only tried messing with the variables.
Here are my variables etc:
tCountTimer = new Timer(100);
tCountTimer.addEventListener(TimerEvent.TIMER, timerTickHandler);
timerCount = 0;
tCountTimer.start();
Here are my Functions to Count:
private function timerTickHandler(e:Event):void
{
timerCount += 100;
toTimeCode(timerCount);
}
private function toTimeCode(milliseconds:int):void
{
this.milliseconds = milliseconds;
//create a date object using the elapsed milliseconds
time = new Date(milliseconds);
//define minutes/seconds/mseconds
minutes = String(time.minutes + 5);
seconds = String(time.seconds);
miliseconds = String(Math.round(time.milliseconds) / 100);
//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+ minutes : minutes;
seconds = (seconds.length != 2) ? '0' + seconds : seconds;
//display elapsed time on in a textfield on stage
playScreen.timeLimitTextField.text = minutes + ":" + seconds;
}
It counts up perfect but just can't get it to time down from 5 minutes. All help is much appreciate.
Didn't test that, but I hope the idea is absolutely clear.
// Remember the time (in milliseconds) in 5 minutes from now.
var endTime:int = getTimer() + 5 * 60 * 1000;
// Call update function each frame.
addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(e:Event):void
{
// How many milliseconds left to target time.
var aTime:int = endTime - getTimer();
// Fix if we are past target time.
if (aTime < 0) aTime = 0;
// Convert remaining time from milliseconds to seconds.
aTime /= 1000;
// Convert the result into text:
// aTime % 60 = seconds (with minutes stripped off)
// aTime / 60 = full minutes left
var aText:String = ze(aTime / 60) + ":" + ze(aTime % 60);
// Do whatever you want with it.
trace(aText);
}
// Function to convert int to String
// and add a leading zero if necessary.
function ze(value:int):String
{
return ((value < 10)? "0": "") + value.toString();
}