2 Replies Latest reply on Apr 23, 2010 6:40 AM by Ned Murphy

    Creating a Timer, but having issues...

    TobinShields

      Hey! I'm new to the fourms, and fairly new to Flash. I am making a game where I want to have a timer running over each level (a total of 4). When the user has finished the game, I want the timer to stop and then display their time.


      I have 2 dynamic text boxes with the varibles set to them as "Seconds" and the other as "Min". Here is the ActionScript I have for it right now:



      var Seconds = 0;

      var Min =0;


      function timer():Void

      {

      _root.Seconds += 1;

      }


      setInterval(timer, 1000)


      All this is doing for me, is giving me one box that counting upwards forever. From what I have learned, it is a lot better to try and write AS out as if your were speaking first, then try to code it. Here is (in theroy) what I want to do next:

      if var 'seconds' = 60 (one minute), then reset var 'seconds' back down to zero. Then, add one to var Min.

      How can I code this?

      Then, after the user has reached the 4th level and gotten to the end, how would I stop the timer?


      Thank you!
      Tobin
        • 1. Re: Creating a Timer, but having issues...
          Ned Murphy Adobe Community Professional & MVP

          I'd recommend you avoid using the variable aspect of textfields and just assign an instance name to them and assign the values to them using the text property of the textfields.  But see if it works out for you for now.

           

          var Seconds = 0;

          var Min = 0;


          function timer():Void

          {

                Seconds += 1;
                if(Seconds == 60){
                      Seconds = 0;
                      Min += 1;
                }

          }


          var intervalID = setInterval(timer, 1000);

           

           

          When you get to the fourth level and want to stop the interval, use...

           

          clearInterval(intervalID);

          • 2. Re: Creating a Timer, but having issues...
            Ned Murphy Adobe Community Professional & MVP

            Another approach to this would be to use the getTimer() function, where you establish a start time from the first execution of it, and then update the current value of it at regular intervals, converting the difference between the current and the start values into minutes and seconds.  This will probably give you a more accurate timing element if it is critical to your design.