Skip to main content
Participating Frequently
May 16, 2006
Question

need countdown timer for game

  • May 16, 2006
  • 7 replies
  • 1228 views
i'm making a shooting game i need a timer that counts down and then when it gets to 0 it goes to a certain frame. This is the code i'm using:
var seconds:Number=10;
time_txt.text="";

function countDown(){
var time=seconds--;
time_txt.text=time;
if(time=0){
gotoAndStop("lose");
clearInterval(counter);
}
}

counter=setInterval(countDown, 1000);

can any one help
This topic has been closed for replies.

7 replies

kglad
Community Expert
Community Expert
September 16, 2023

locked secondary to age and spam attraction.

kglad
Community Expert
Community Expert
August 19, 2023

@mit317667020y6y 

 

as3 or canvas/html5?

what exactly are you looking for?

Craig Grummitt
Inspiring
June 1, 2006
i just created a text box, named it time_txt, embedded fonts, created a frame label at frame 15 called "lose" and then pasted the above code into frame 1.
when i exported the movie, the text box counted down from 10 to -10, and then the timer stopped on -10 and the movie went to frame 15.
what exactly is the problem?
Craig Grummitt
Inspiring
May 29, 2006
i'm still trying to understand why you changed your if statement to if (time==-10)...!

the code above works for me, so i think your problem lies in what other code you have in your project.

depending on what you mean by "reset the timer":

the clearInterval should turn off the interval(which otherwise updates the timer periodically)

the line var seconds:Number = 10; resets the timer to 10...

c
spudlessAuthor
Participating Frequently
May 30, 2006
I changed the code to time==-10 because it was counting down from 0 not 10. Anyways thanks for trying to help craig.

Cya Spud
spudlessAuthor
Participating Frequently
May 30, 2006
I figured out the problem. The countdown timer doesn't stop counting after it reaches the specified time (in this case -10). Does anyone know how to make it stop.
Craig Grummitt
Inspiring
May 16, 2006
yes that would work too Nicwinta. i'm confused however about why your original code doesn't work spudless? once i added the extra equal sign to that line, it worked fine for me... see below..

by the way, i doubt it's counting up - more likely i think it's in the negative continuing to count down and you haven't embedded hyphen in your font.
spudlessAuthor
Participating Frequently
May 17, 2006
It works now. I just had to change the code from if(time==0); to if(time==-10)

Thanks for all your help craig and nicwinta

cheers spudless
spudlessAuthor
Participating Frequently
May 29, 2006
hey guys i run into a problem. when game either times out or you finish the level. the timer begins to count from wherever it left off eg 6. does anyone know code to reset the timer
May 16, 2006
spudless,

I am very new to flash scripting but got the following code to work (I think)!

var seconds:Number = 10;
var time:Number = seconds;
function countDown() {
if (time==0) {
gotoAndStop("lose");
clearInterval(counter);
}
time_txt.text = time;
time = --seconds;
}
counter = setInterval(countDown, 1000);
spudlessAuthor
Participating Frequently
May 16, 2006
it only counts up. Plus it doesn't stop counting it just keeps going.

Can anyone help

Thanks Spudless
Craig Grummitt
Inspiring
May 16, 2006
looks like you've got things pretty well under control.
you didn't say what the symptoms of the problem with your code is, but one thing that needs fixing is the following line:

if(time=0){

should be:

if(time==0){

a subtle but important difference!
craig
spudlessAuthor
Participating Frequently
May 16, 2006
sorry mate the problem is that it counts up instead of down and it starts at 1. So it never can reach zero.

Any help will be great

Spudless