Home‎ > ‎

Timers


If you want to make your game more exciting, you can also add a timer. It can be a countdown timer where you start with a time such as 60 seconds, then count down to zero. Or it can be a count up timer which tracks how long it takes the user to do something.

How We Do It

To make a timer, we need a global variable - "timer" (Remember, global variables are variables that are available everywhere. They are 'global' in nature)

The variable "timer" will keep track of the time left. The countdown timer will stop when it reaches zero. You have to stop the countup timer yourself. You can add a check for it anywhere in your code that you do your actions.

We will create a message handler called "updateTimer" that will check if the time has expired and if not, subtract one from its time. It will then send a message to itself every 1 second so that it continues counting down.

We will start the timer when the card is opened by putting code in the "openCard" handler. You can change this and put this code in your "Start" button or wherever you start your game.

A Simple CountDown Timer:

1. Add a text field for the timer - call it "timer"

2. Put this on the card script:

     on countDown

           subtract 1 from field "timer"

           if field "timer" = 0 then

                answer "Time's up"

           else

                send "countDown" to me in 1 sec

           end if

        end countDown


3.Add a Start button
Put this on the START button script:

     on mouseUp

           put 20 into field "timer"

           countDown

      end countDown



Basic Timers: 

1. Simple Time Counter - Start/End - (not on screen, in pop-up Dialog box)
This is a simple timer that you can have in your program. The user will not see it counting but will see their time when it ends (in a pop-up box using the Answer command).
  • When you are ready to start timing, add the following lines (like where you move or read the keyboard):
        global tStart,tEnd
        put the seconds into tStart
      --------------------
  • When you are done or the user finishes the game, do the following lines:
         global tStart,tEnd
         put the seconds into tEnd
         answer "You took " & tEnd-tStart & " seconds"
  • or you can shorten that with the following when the user finishes the game:
         global tStart         
         answer the seconds - tStart
 
   Simple Time Counter (on screen)
   This does the above but also puts the secs on the screen for the player to see. 
   The differences (and new code) are in Green
  • Add a "Text Entry" field called "myTime"

  • When you are ready to start timing, add the following lines:
        global tStart,tEnd
        put the seconds into tStart
        put 0 into field "myTime"
  • Wherever you move the player, add the following code: (e.g. in "On ArrowKey")
         global tStart
         put the seconds - tStart into the field "myTime"
     --------------------
  • When you are done or the user finishes the game, do the following lines:
         global tStart,tEnd
         put the seconds into tEnd
         answer "You took " & tEnd-tStart & " seconds"
  • or you can shorten that with the following when the user finishes the game:
         global tStart         
         answer the seconds - tStart

    note: If you are testing their reactions and seconds are too long, you can use "ticks" instead of "seconds" in the above commands. (Ticks are 1/6 of a second - there are 6 ticks in a second. You can divide by 6 to get seconds)

  
2. Button - Simple Count Up Timer (with a timer field on screen)
This is a simple button driven counter that will count up starting with zero.
  • Add a "Text Entry" field and name it (e.g. "timer")
  • Add a button named "Start"  and add the following code to its script
       global sFlag
        on mouseUp
              put 0 into field "timer"
              put true into sFlag
              send "updateTimer" to card "my card" (or whatever you call your card)
        end mouse Up   
  • Add a message handler called "updateTimer" to update the time by adding the following script to the top of your card:
        global sFlag    

         on updateTimer

              add 1 to field "timer"

              if sFlag is true then 

                   send "updateTimer" to me in 1 second

              end if

        end updateTimer 

  • When you want to stop the timer just add the lines to a button or Intersect command:

            global sFlag

            put false into sFlag


        where you want. For instance:

             - in a "Stop" button

             - in a finish-line button (that may be invisible (in a "if intersect (obj1, obj2, 255) then...")

             - in code when you get all the prizes, or a top score



3. Button - Simple Count Down Timer
This is a simple button driven counter that will countdown from 15 seconds to 0.  
  • Add a label field and name it (e.g. "timer")
  • Add a button named "Start"  and add the following code to its script
       global sFlag
        on mouseUp
            put 15 into field "timer"
            put true into sFlag
            send "updateTimer" to card "my card"
        end mouse Up   
  • Add the following script to the top of the card:
        global sFlag    

         on updateTimer

            subtract 1 from field "timer"

            if field "timer" = 0 then 

                put false into sFlag

                answer "Game Over"

            end if

          if sFlag is true then 

                send "updateTimer" to me in 1 second

            end if

        end updateTimer 

  • When you want to stop the timer just add the line:

            global sFlag

            put false into sFlag


        where you want. For instance:

             - in a "Stop" button

             - in a finish-line button (that may be invisible)

             - in code when you get all the prizes, or a top score


Basic Timers: (with "Start" and "Stop" buttons to control the action)

4. Button - Fancy Count Down Timer with 1 Start/Stop button
This is a simple button driven counter that will countdown from 15 seconds to 0. Once started, the button name changes to "Stop" and will function to stop the timer when pressed.
  • Add a label field and name it (e.g. "timeLeft")
  • Add a button named "timer" and make the Label say "Start"
  • Add the following script to the top of the card:
        global countDownValue    

         on timerCountDown

               subtract 1 from CountDownValue

               if countDownValue > 0 then

                      put countDownValue into field "timeLeft"

                      send "timerCountDown" to me in 1 second

               else

                      put 0 into countDownValue

                      put countDownValue into field "timeLeft"

                      set the label of button "Timer" to "Start"

              end if

         end timerCountDown


  • Add the following code to your "Start" button       
         on mouseUp
               if the label of button "timer" = "Start" then
                      set the label of button "timer" to "Stop"
                      put 15 into countDownValue    
                      put countDownValue into field "timeLeft"
                      send "timerCountDown" to me in 1 second
              else
                      set the label of button "timer" to "Start"
                      put 0 into countDownValue
                      put countDownValue into field "timeLeft"
              end if
       end mouseUp


 

5. Button - Fancy Count Up Timer ( 1 button that changes from "Start" to "Stop")
  • Add a label field and name it (e.g. "timeUsed")
  • Add a button named "timer" and make the Label say "Start"
  • Add the following script to the top of the card:
        global countUpValue    

         on timerCountUp

               add 1 to countUpValue

               if countUpValue > 0 then

                      put countUpValue into field "timeUsed"

                      send "timerCountUp" to me in 1 second

               else

                      put 0 into countUpValue

                      put countUpValue into field "timeUsed"

                      set the label of button "timer" to "Start"

              end if

         end timerCountUp


  • Add the following code to your "Start" button       
         on mouseUp
               if the label of button "timer" = "Start" then
                      set the label of button "timer" to "Stop"
                      put 0 into count
UpValue     
                      put count
UpValue into field "timeUsed"
                      send "timerCount
Up" to me in 1 second
              else
                      set the label of button "timer" to "Start"
                      
put countUpValue into field "timeUsed"
 
             end if
       end mouseUp

    

That's all there is to it. Now you can make your games more time-competitive

Comments