Johnny G Junior's NoteBook Treasures: October 2008

Sunday, October 5, 2008

Simple CountDown Timer - Game Stuff



Step 1: create 2 layers. one name Actions and the other layer create a dynamic Text box and name it "Timer" in the Var field.


Setp 2: Lets script it up on the blank frame on the Actions layer (i will try to break it down as easy as i can).

Timer = 120;


This will set the Timer start time and it will also (Because it is NAMED the SAME as the dynamic txt field) place 120 in the txt box you created.

Step 3: Create a function that will count the timer down. this function we will repeat over and over again until we tell it to stop.

CountDown function() {
Timer--;
}


[YIPPY WE DID IT - how do we repeat it....]

Step 4: Create a repeater using setInterval. This will repeat a function over and over again in a set time.


CounterInt = setInterval(CountDown, 1000);


setInterval Usage = setInterval(function name,Interval Number)

great. now if you preview the movie (alt + enter) you should see a working count down. The PROBLEM is that we never set a stop for the countdown. Basically when it hits 0 it will keep counting down. so for this, we have to create a clear interval. Lets revisit step 3 and add an IF clause.

REVISITING STEP 3:

CountDown function() {
Timer--;
if (Timer == 0) {
clearInterval(CounterInt);
}
}


clearInterval will now stop the setInterval we created in step 4.

the final script should look like this.

Timer = 120;

CountDown= function(){

if (Timer == 0){
clearInterval(CountInt);
} else {
Timer --;
}
}
CounterInt = setInterval(CountDown,1000);



Valah!

if you would like to go the opposite way... counting up. change Timer-- to Timer++;

be good.

[Paste in Place] FLASH

Control Shift V = PASTE IN PLACE...... this keyboard shortcut is golden.

-JB-