Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

24 hours looping countdown clock

Guest
Oct 14, 2010 Oct 14, 2010

Hi i am trying to create a 24 hour countdown clock that will loop and play backwards counting it self back down to 00:00:00:00, being new to flash As3 i am struggling to create this animation

the AS3 code i have put into my actions is as follows:

var timer:Timer = new Timer(1000);
var currentSeconds:int = 86400;
var milliseconds:int = 86400000;
var result:String = "";

timer.addEventListener(TimerEvent.TIMER, countDown);

function countDown(e:TimerEvent):void {

currentSeconds -= 1;
milliseconds -= 1000;

var minutes:int = (currentSeconds / 60 >= 1)?currentSeconds / 60:0;
var hours:int = minutes*60;
var seconds:int =
currentSeconds < 59?currentSeconds:currentSeconds - (int(currentSeconds / 60)*60);

result = ((hours >= 10)?hours:"0"+hours)+":"+((minutes >= 10)?minutes:"0"+minutes) +":" + ((seconds >= 10)?seconds:"0"+seconds)+":"+milliseconds/100+"";

}

When i press to test the movie the numbers are still 00:00:00:00

if any one could shed some light on what i am doing wrong it would be very helpfull !

below i have attached a link to an image to give a better idea of where i am at with this animation:

http://img145.imageshack.us/img145/8/screenshot20101013at224.png

thanks in advance

tom

TOPICS
ActionScript
9.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2010 Oct 14, 2010

I am not sure what/where you intend to display the result (or what you expect it to appear as), but you need to start() the timer to get things rolling.  If you are intending to display the value in that textfield, then you need to assign it to that textfield's text property... as in...  tfieldname.text = result;

You probably need to check your math too...  var hours:int = minutes*60;  That's not gonna get you hours, more like seconds, and I haven't checked any of the rest of it

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2010 Oct 14, 2010

You don't have to jump through all these math hoops. People forget that there is Date class that can be utilized in practically any situations where date/time are at play.

Here is a code that displays 24 hour countdown in a TextField. Note no calculations of hours, minutes or seconds or converting them to string is involved:

var result:String = "";
var date:Date = new Date(null, null, null, 24, 0, 0, 0);

var timeDisplay:TextField = new TextField();
timeDisplay.autoSize = TextFieldAutoSize.LEFT;
timeDisplay.multiline = timeDisplay.wordWrap = false;
timeDisplay.border = true;
timeDisplay.x = timeDisplay.y = 20;
timeDisplay.text = "00:00:00";

addChild(timeDisplay);

var timer:Timer = new Timer(250);
timer.addEventListener(TimerEvent.TIMER, countDown);
timer.start();

function countDown(e:TimerEvent):void {
     date = new Date(date.setTime(date.getTime() - timer.delay));
     result = date.toTimeString().replace(/\s+GMT\-\d+/, "");
     timeDisplay.text = result;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2010 Oct 14, 2010

Oh, if you want to display words hours, minutes, etc., you can set result as following:

result = "hours: " + date.toTimeString().replace(/\s+GMT\-\d+/, "").replace(/\:/, " minutesX ").replace(/\:/, " secondsX ").replace(/X/g, ":");

Or, to add milliseconds:

result = "hours: " + date.toTimeString().replace(/\s+GMT\-\d+/, "").replace(/\:/, " minutesX ").replace(/\:/, " secondsX ").replace(/X/g, ":") + " milliseconds: " + date.getMilliseconds();

I am toolazy to come up with a more efficient RegExp 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 15, 2010 Oct 15, 2010

Thats fantastic thanks you very much for your help!

do you no how i could change the design of the type on the swf as i need to have it in a dark grey arial font as shown in the picture.

once again thanks for all your help so far!!!

tom

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 15, 2010 Oct 15, 2010
LATEST

You are welcome.

Please mark this thread answered if you can.

Design question is a separate topic that, I believe, should be a separate thread.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines