COUNTDOWN TO SPECIFIC TIME as3 |
This not very complicated tutorial shows how to create a nice countdown feature for you web page or any other flash project.
Useful for upcoming events, or undergoing maintance. In this case it is set to count time to the next christmas, ofcourse you can set whatever date you would like to countdown to. At the end we should get something similar to this :
Open new actionscript 3.0 flash document. The entire countdown is based on one dynamic text field, everything around it is puredecoration I will leave on you creativity.
Insert new layer. You should get two now. Insert three blank keyframes in the top one, and one 2 frames long on the second. Select the second layer and insert dynamic text area. Set its instance name
to timeString. Now select layer one, frame one, press F9 (open action pannel) and insert following script.
var christmas:Date = new Date(2012, 11, 25, 0, 0, 0, 0); var now:Date = new Date(); if (now >= christmas) { gotoAndStop(3); } var timeDiff:Number = christmas.getTime() - now.getTime(); var seconds:Number = Math.floor(timeDiff / 1000); var minutes:Number = Math.floor(seconds / 60); var hours:Number = Math.floor(minutes / 60); var days:Number = Math.floor(hours / 24); hours %= 24; minutes %= 60; seconds %= 60; var hourZero:String = new String(); var minuteZero:String = new String(); var secondZero:String = new String(); if (seconds < 10) { secondZero = "0"; } else { secondZero = ""; } if (minutes < 10) { minuteZero = "0"; } else { minuteZero = ""; } if (hours < 10) { hourZero = "0"; } else { hourZero = ""; } var timeRemaining:String = days +":"+ hourZero + hours +":" + minuteZero+ minutes + ":" +secondZero+ seconds; timeString.text = timeRemaining;
Line 1 creates the date you want to countdown to. In this case christmas morning (year, month-1, day, hours, minutes, seconds and milliseconds). Line 3 stores current date in new variable. Lines 4-6 checks if its before or past
christmas. If its after set up date, this if condition jumps to frame 3. Line 8 counts the difference between current time and christmas in milliseconds. Lines 9-16 converts time to christmas in
milliseconds to days, hours, minutes, and seconds. Since date() does not return double digit number while under 10, lines 18-48 covers this problem by adding 0 to hours, minutes and seconds while they
are under 10. Line 50 gathers this times and converts them into one string. Line 52 then sends this string to the dynamic text field and displays it on the stage.
|