You're just trying to increase the value of a text field by a certain amount every second? Easy enough...
First step up a dynamic text field on your stage. Let's give it an instance name of "theNumber". Enter whatever starting number you wish. That's all that you'll have to do on the stage. The rest is all actionscript.
Here is the final code. I'll break it down below.
Code:
function countdown() {
theNumber.text = theNumber.text - 1;
}
setInterval(countdown, 1000);
The workhorse function is called countdown. Basically it takes whatever is in the text field (theNumber.text) and then subtracts one from it every time it is run. The setInterval() method runs the countdown function every 1000 milliseconds (or every second).
Thus, you have your countdown. If you're trying to do a full time countdown, it gets a little more complicated. Here is an example of what it will look like in the end. You'll need separate dynamic text fields for each unit. In this example, I've given then an instance name of days, hours, minutes, etc... The ActionScript goes a little like this:
Code:
function countdown() {
if(milliseconds.text == 00) {
milliseconds.text = 60;
seconds.text = seconds.text -1;
}
if(seconds.text < 0) {
seconds.text = 59;
minutes.text = minutes.text - 1;
}
if(minutes.text < 0) {
minutes.text = 59;
hours.text = hours.text -1;
}
if(hours.text < 0) {
hours.text = 23;
days.text = days.text -1;
if(days.text < 10) {
days.text = "0" + days.text;
}
}
milliseconds.text = milliseconds.text - 1;
}
setInterval(countdown, 1);
Note: This isn't necessarily the most robust way of doing things, but it illustrates the technique. For the above to work, you'll have to set a framerate at approx. 10fps .
Bookmarks