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

need countdown timer for game

New Here ,
May 15, 2006 May 15, 2006

Copy link to clipboard

Copied

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
TOPICS
ActionScript

Views

901

Translate

Translate

Report

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
Advisor ,
May 15, 2006 May 15, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
May 15, 2006 May 15, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
May 16, 2006 May 16, 2006

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

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
New Here ,
May 16, 2006 May 16, 2006

Copy link to clipboard

Copied

it only counts up. Plus it doesn't stop counting it just keeps going.

Can anyone help

Thanks Spudless

Votes

Translate

Translate

Report

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
Advisor ,
May 16, 2006 May 16, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
New Here ,
May 16, 2006 May 16, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
May 28, 2006 May 28, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Advisor ,
May 28, 2006 May 28, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
May 29, 2006 May 29, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
May 29, 2006 May 29, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
New Here ,
May 31, 2006 May 31, 2006

Copy link to clipboard

Copied

just in case this might help anyone tries to help
here is the code i'm using for the game:
stop();
//Set global highScore variables
highScore = 0;
//Set the properties for the game elements
_root.lastframe = false;
//Stop bullets
//See AS for bullet MC for details
setProperty(_root.crosshair, _visible, true);
setProperty(_root.bullet, _visible, false);
//
//Individual destroyed target images hidden until needed
setProperty(_root.target1d, _visible, false);
setProperty(_root.target2d, _visible, false);
setProperty(_root.target3d, _visible, false);
//As above. Individual highlighted target images hidden until needed
setProperty(_root.target1Image, _visible, false);
setProperty(_root.target2Image, _visible, false);
setProperty(_root.target3Image, _visible, false);
mouse.hide();
//hide mouse to use cross-hairs
//select a target to shoot at random then add that target
//to the array of selected targets so that it isn't used
//again in this game.
//When the number of direct hits against the selected
//target has been reached. Find another random target.
//Check whether it exists in the array. If yes, select
//again. If no, make visible.
_root.targets = new Array();
//create array on the main
//timeline so that it is
//easily accessed by all MCs
_root.targets[0] = "target1";
_root.targets[1] = "target2";
_root.targets[2] = "target3";
//3 targets we are using in
//tutorial
n = _root.targets.length;
//get length of array
ran = random(n);
//get random number location to select from array
target = _root.targets[ran];
//show random array location
_root.activeTarget = target;
//create target variable
setProperty("_root."+target+"Image", _visible, true);
//show target
_root.targets.splice(ran, 1);
//remove the random element
//from the array. splice
//the random number found and
//to the depth of one so only
//one element is removed.
//-------------------------------------------------------//

var seconds:Number = 10;
time_txt.text = "";
function countDown() {
var time = seconds--;
time_txt.text = time;
if (time == -10) {
gotoAndStop("lose");
clearInterval(counter);
}
}
counter = setInterval(countDown, 1000);


any help will be appreciated
Spud

Votes

Translate

Translate

Report

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
Advisor ,
May 31, 2006 May 31, 2006

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 19, 2023 Aug 19, 2023

Copy link to clipboard

Copied

@mit317667020y6y 

 

as3 or canvas/html5?

what exactly are you looking for?

Votes

Translate

Translate

Report

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
Community Expert ,
Sep 15, 2023 Sep 15, 2023

Copy link to clipboard

Copied

LATEST

locked secondary to age and spam attraction.

Votes

Translate

Translate

Report

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