12 Replies Latest reply on May 31, 2006 5:22 PM by Craig Grummitt

    need countdown timer for game

    spudless
      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
        • 1. Re: need countdown timer for game
          Craig Grummitt Level 3
          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
          • 2. Re: need countdown timer for game
            spudless Level 1
            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
            • 3. Re: need countdown timer for game
              Nicwinta
              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);
              • 4. Re: need countdown timer for game
                spudless Level 1
                it only counts up. Plus it doesn't stop counting it just keeps going.

                Can anyone help

                Thanks Spudless
                • 5. need countdown timer for game
                  Craig Grummitt Level 3
                  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.
                  • 6. Re: need countdown timer for game
                    spudless Level 1
                    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
                    • 7. Re: need countdown timer for game
                      spudless Level 1
                      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
                      • 8. Re: need countdown timer for game
                        Craig Grummitt Level 3
                        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
                        • 9. Re: need countdown timer for game
                          spudless Level 1
                          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
                          • 10. Re: need countdown timer for game
                            spudless Level 1
                            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.
                            • 11. Re: need countdown timer for game
                              spudless Level 1
                              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
                              • 12. Re: need countdown timer for game
                                Craig Grummitt Level 3
                                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?