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 count up 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 and in the "startGame" handler.. You can change this and put this code in
your "Start" button or wherever you start your game.
Game Loop Timers:
1. Count Down Timer This is a timer that starts at a certain time and counts down to zero. This code is for a game that has a Game Loop.
- Go to your card where you want the timer, add a text entry field and name it (e.g. 'Time Left')
- Open up the card script and add the following at the top of the script:
global timer- Add
the following code so that it starts the timer when the card is opened,
or put it somewhere else where you want to start a timer (like a
"Start" button)
On the card script,
add the code to start the timer when the card is first opened
on openCard
put empty into field "Time Left" put 60 into timer updateTimer end openCard or add the code to the card script to start the game on startGame put empty into field "Time Left" put 60 into timer updateTimer end startGame
- Add the "updateTimer" handler on the card script
on updateTimer if timer > 1 then put timer into field "Time Left" put timer - 1 into timer send "updateTimer" to me in 1 second end if end updateTimer
- In any object scripts that refer to timer or halt, add the following code:
global timer
- When you want the timer to stop counting, set it to be negative
put -1 into timer
- When you go to another card, you should stop the timer - set it to be negative
put -1 into timer
2. Count Up Timer For a count down timer, you can use the above code but you have to make a few changes. You need to:
- Go to your card where you want the timer, add a text entry field and name it (e.g. 'Time Used')
- Open up the card script and add the following at the top of the script:
global timer
- Add
the following code so that it starts the timer when the card is opened,
or put it somewhere else where you want to start a timer (like a
"Start" button)
On the card script,
add the code to start the timer when the card is first opened
on openCard put empty into field "Time Used" put 0 into timer updateTimer end openCard
or add the code to the card script to start the game
on startGame put empty into field "Time Used" put 0 into timer updateTimer end startGame
- Add the "updateTimer" handler on the card script
on updateTimer if timer >= 0 then put timer into field "Time Used" put timer + 1 into timer send "updateTimer" to me in 1 second end if end updateTimer
In any object scripts that refer to timer or halt, add the following code: global timer
- When you want the timer to stop counting, set it to be negative
put -1 into timer
- When you go to another card, you should stop the timer - set it to be negative
put -1 into timer
That's all there is to it. Now you can make your games more time-competitive
|
|