-
Countdown timer with tenths of a second
Hi there,
I have a countdown timer that works well counting down a minute, before it does something.
I'd like to add counting of tenths of a second to this too, but don't know where to start.
Please help if you can.
Actionscript Code:
var turnTimerSeconds:int = 60; var turnTimerDuration:int = 1000; // 1 second var turnTimerRepeat:int = 60;
var turnTimer = new Timer(turnTimerDuration,turnTimerRepeat);
turnTimer.addEventListener(TimerEvent.TIMER, turnTime);
function turnTime(e:Event) { var totalSecondsLeft:Number = (MovieClip(this.root).turnTimerSeconds) - turnTimer.currentCount; main.countdown_timer.counting.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 sSeconds; }
turnTimer.addEventListener(TimerEvent.TIMER_COMPLETE, turnDone);
function turnDone(e:TimerEvent):void { turnTimer.reset(); turnTimer.stop(); game_stage = OUT_OF_TIME; }
THANKS!
-
There is no need for going to the root to get turnTimerSeconds. It's already defined in the scope of that function.
Your timer is set to tick once a second. You need it to tick 10 times a second. That means that your turnTimerDuration will be 100 rather than 1000, and your repeat count will by 600 rather than 60.
You can keep the logic in turnTime pretty much the same, but you'll be calculating totalTenthsLeft rather than totalSeconds. Adjust your formatting function accordingly.
Code:
var turnTimerTenths:int = 600;
var turnTimerDuration:int = 100; // 0.1 second
var turnTimerRepeat:int = turnTimerTenths;
var turnTimer = new Timer(turnTimerDuration,turnTimerRepeat);
turnTimer.addEventListener(TimerEvent.TIMER, turnTime);
function turnTime(e:Event) {
var totalTenthsLeft:Number = turnTimerTenths - turnTimer.currentCount;
main.countdown_timer.counting.text = timeFormat(totalTenthsLeft);
}
function timeFormat(tenths:int):String {
var seconds:int = Math.floor(tenths/10);
var minutes:int;
var sMinutes:String;
var sSeconds:String;
var sTenths:String;
if (seconds > 59) {
minutes = Math.floor(seconds / 60);
sMinutes = String(minutes);
} else {
sMinutes = "";
}
sSeconds = String(seconds % 60);
sTenths = String(tenths % 10);
if (sSeconds.length == 1) {
sSeconds = "0" + sSeconds;
}
return sMinutes +":"+sSeconds+":"+sTenths;
}
turnTimer.addEventListener(TimerEvent.TIMER_COMPLETE, turnDone);
function turnDone(e:TimerEvent):void {
turnTimer.reset();
turnTimer.stop();
game_stage = OUT_OF_TIME;
}
I don't know why you were calculating sMinutes and then doing nothing with it.
-
Thanks very much indeed. Makes perfect sense, when so well explained.
The minutes were there from an earlier version of timer.
Really appreciate it!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|