View Full Version : [AS3] Progressively Quicker CountDown Timer
Affimage
02-25-2009, 11:04 PM
I am looking for help on making a basic 20 second count down timer increase in the speed in which it countsdown by as the player progresses.
I am running a code like
var speed:Number = 1000;
var timer:Timer = new Timer(speed, 20);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
var totalSecondsLeft:Number = 20 - timer.currentCount;
myText.text = timeFormat(totalSecondsLeft);
}
function timeFormat(seconds:int):String {
var sSeconds:String;
if (seconds == 0) {
resettimer();
}
if (seconds > 21) {
sSeconds = String(seconds % 60);
} else {
sSeconds = String(seconds);
}
if (sSeconds.length == 1) {
sSeconds = "0" + sSeconds;
}
return ":" + sSeconds;
}
function resettimer() {
timer.reset();
timer.start();
}
I have this variable declaring that it changes every one second:
var speed:Number = 1000;
And I have tryed to figure out a way that every time the user wins a round or something (I will figure out how I wanna set it up), the variable will decrease by 100, which will increase the speed the clock ticks down at. I however cannot figure out how to get this to work.
/help.
bluemagica
02-26-2009, 05:19 AM
function resettimer() {
speed-=100;
timer.reset();
timer.start();
}
ronny.depp
10-05-2009, 11:00 PM
Hi Everyone, ;)
First of all I would like to thank Mazoonist (http://www.actionscript.org/forums/member.php3?u=43726). I am working on an project of Online Event Broadcasting for a Lottery online.
So I needed to check & also Display the time remaining until Next Event to be Broadcasted Live. Maz's Code-Snippet got me going & saved my time. :) thanks MAz00ni$t !!!
Original Thread: http://www.actionscript.org/forums/showthread.php3?p=929804
Here I have Extended MAZ'z version of CountDownTimer.
Description: Here I am posting the New Extended Version of CountDownTimer aka CountDown Timer.
I wanted to elaborate on the Basic for newbies. So I have got it in a bit more explanatory way.
I will Post my Final Application Code as well soon ;)
It includes ability to Calculate and Display the Time Parameter(provided in seconds), in the form of Days, Hours, Minutes, Seconds remaining.
Actually, currently it just takes the number of second provided by you, as you set it manually in the source code, and converts them into Days, Hours, Minutes and Seconds. And then outputs it as a String in the designated TextField.
Purpose of this Post: is to Elaborate Timer class's capabilities and basic idea on how to use them. While also describing a simple way of making a CountDown Timer.
How To Use: You are free to use and modify/customize it according to your requirements. You can replace the value of variable secs this way: var secs:int = 86400*3;/*Sets the Time to 86400*3 secs. meaning {3 Days} as there
are 86400 seconds in 24hours aka 1 Day.
//And there are 86400000 Milliseconds in 1 Day aka 24 hours.
//1 Hour = 3600 seconds = 3600000 Milliseconds
*/ Change the Value of var secs with some Dynamic Variable value by doing some real calculations for Date/Time by Playing around with Date Object's getTime() method.
var secs:int = (new Date()).getTime()/1000;// Convert the Time returned in Milliseconds to Seconds.
//DateObj.getTime() methods returns Milliseconds elapsed since
//January 1, 1970 00:00:00 GMT/UTC, up to the Date/Time set by DateObj you create.
Remarks/Updates: I have left some Unwanted lines of Trace Statements, extra Variable Declarations.
So here I'm going to Post the more Dirtier Version (with more Comments for Explanations) of
ActionScript 3 code for Frame1:
var newDate:Date = new Date();
var seconds:int = int((newDate.getTime()/1000));
//
var secs:int = 86400*3;//Sets the Time to 86400*3 secs. meaning {3 Days} as
//there are 86400 seconds in 24hours aka 1 Day.
//And there are 86400000 Milliseconds in 1 Day aka 24 hours.
//1 Hour = 3600 seconds = 3600000 Milliseconds
myText.text = timeFormat(secs);/* call the timeFormat() function to Flush/Display the Actual Time
Remaining before Timer Starts or any Time elapses.
Flushes this: "3 Day(s) : 00:00:00" for a Second.
//
Commenting-out/Removing this line will result in Flash showing output only after 1 second passes.
The Display output will be starting from: "2 Day(s) : 23:59:59" only after 1 Second passes/elapses.
It only display output after 1 second has passed.
*/
var timer:Timer = new Timer(1000, secs);// new Timer(DelayInMilliseconds, TotalNumberOfTimesToTrigger)
timer.addEventListener(TimerEvent.TIMER, countdown); // TimerEvent.TIMER event is fired ...
//everytime Timer is Invoked after the Delay period. Capture this event and assign a function to be
//called when Timer is Invoked each time, by using the addEventListener
//(EventToListen,FunctionToCall) method on Timer object.
timer.start();//call start() method to get it started
function countdown(event:TimerEvent) {
var totalSecondsLeft:Number = (timer.repeatCount) - (timer.currentCount);
myText.text = timeFormat(totalSecondsLeft);
//trace("counter: " + timer.currentCount);
// {timer.currentCount} property returns: Number of times Timer has already been Invoked.
//Actually it's a counter to count number of times Timer was invoked until now.
// {timer.repeatCount} property returns: Total number of times Timer will be Invoked.
//It's set when the Timer Object is created, as the second parameter to the TimerObj's
//constructor function.
// {timer.delay} property returns: Milliseconds to delay the Invoking of the Timer.
//You can call it an Interval between Invoking attempts. Set at the time of creation of
//TimerObj, as a first parameter.
}
function timeFormat(seconds:int):String {
var days:int;
var hours:int;
var minutes:int;
var sDays:String;
var sHours:String;
var sMinutes:String;
var sSeconds:String;
if(seconds > 59) {
minutes = Math.floor(seconds / 60);
if(minutes > 59) {
hours = Math.floor(minutes / 60);
if(hours >= 23) {
days = Math.floor(hours / 24);
sDays = String(days);
sHours = String(hours % 24);// use of %(modulus) to get only the Remainder of the
//Division. because we only need the remaining part of time after extracting whole numbers for Days.
//And now we need the remainder to extract Hours. Then remainder of that to extract Minutes.
//
// Then Seconds at last. ;) Skip the Milliseconds part ;D
sMinutes = String(minutes % 60);
sSeconds = String(seconds % 60);
} else {
days = 0;
sHours = String(hours);
sMinutes = String(minutes % 60);
sSeconds = String(seconds % 60);
}
} else {
days = 0;
hours = 0;
sDays = "00";
sHours = "00";
sMinutes = String(minutes);
sSeconds = String(seconds % 60);
}
} else {
days = 0;
hours = 0;
minutes = 0;
sDays = "00";
sHours = "00";
sMinutes = "00";
sSeconds = String(seconds);
}
if(sSeconds.length == 1) {
sSeconds = "0" + sSeconds;
}
if(sMinutes.length == 1) {
sMinutes = "0" + sMinutes;
}
return sDays + " Day(s) : " + sHours + ":" + sMinutes + ":" + sSeconds;
}
trace("Seconds: " + seconds); // just for my Tracing
trace("Date: " + newDate.toDateString()); // just for my Tracing
Source Files: FLA (countDownTimer.fla), SWF (countDownTimer.swf)
Source File Archive: countDownTimer_Extended_FLA_&_SWF.zip (attached)
Source Archive Tags (Keywords): {countDownTimer.zip}, actionscript3, actionscript 3, actionscript, action script, 3, countDownTimer, countdown, timer, countdown timer, count, down, seconds, milliseconds, minutes, hours, days, weeks, months, years, centuries, millenium, event tracker, broadcast event, broadcast live event, broadcast live events, live, event, events, broadcast, as, as3, as 3, flash, flash cs3, flash cs4, [as3], cs3, cs4, as3 countdown timer, [as3] countdown timer
Comments: My Complements to All of you Guys who posted Code for different scenarios/variations.
I am also looking forward to the comment from you guys, about how well i do it or how bad. I know it seems more like a tutorial rather than a forum post and I know I should have posted it on my blog instead. But I was not in the mood right now to Login to my Blog. ;)
.
ronny.depp
10-06-2009, 02:39 AM
Hi Everyone, ;)
Original Thread: http://www.actionscript.org/forums/showthread.php3?p=929804
P.S.: Forgotten File Attached.
Source Files: FLA (countDownTimer.fla), SWF (countDownTimer.swf)
Source File Archive: countDownTimer_Extended_FLA_&_SWF.zip (http://www.actionscript.org/forums/attachment.php3?attachmentid=32519&d=1254793145) (attached)
Source Archive Tags (Keywords): {countDownTimer.zip}, actionscript3, actionscript 3, actionscript, action script, 3, countDownTimer, countdown, timer, countdown timer, count, down, seconds, milliseconds, minutes, hours, days, weeks, months, years, centuries, millenium, event tracker, broadcast event, broadcast live event, broadcast live events, live, event, events, broadcast, as, as3, as 3, flash, flash cs3, flash cs4, [as3], cs3, cs4, as3 countdown timer, [as3] countdown timer, adobe flash, flash actionscript 3,
Comments: My Complements to All of you Guys who posted Code for different scenarios/variations.
I am also looking forward to the comment from you guys, about how well i do it or how bad. I know it seems more like a tutorial rather than a forum post and I know I should have posted it on my blog instead. But I was not in the mood right now to Login to my Blog. ;)
sincerely
--
Ronny Depp (http://www.actionscript.org/forums/member.php3?u=89000)
.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.