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:
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).
put the seconds into tStart --------------------
global tStart,tEnd put the seconds into tEnd answer "You took " & tEnd-tStart & " seconds"
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
put the seconds into tStart put 0 into field "myTime"
put the seconds - tStart into the field "myTime" --------------------
put the seconds into tEnd answer "You took " & tEnd-tStart & " seconds"
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.
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
on updateTimer add 1 to field "timer" if sFlag is true then send "updateTimer" to me in 1 second end if end updateTimer
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 This is a simple button driven counter that will countdown from 15 seconds to 0.
on mouseUp put 15 into field "timer" put true into sFlag send "updateTimer" to card "my card" end mouse Up
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
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.
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
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")
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
if the label of button "timer" = "Start" then set the label of button "timer" to "Stop" put 0 into countUpValue put countUpValue into field "timeUsed" send "timerCountUp" 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 |
Home >