GameMaker Version 1.4.1749
1 –
Creating our countdown object
·
Create
an object with name obj_countdown.
2 – Getting
setup
·
In
the countdown object, add a Create event.
·
In
that event, add a Set Variable action (control tab).
o
Name:
timeLeft
o
Value:
15
·
In
that same event, add a Set Alarm action (main2 tab).
o
Number
of steps: 30
o
Alarm
#: Alarm 0
Notes:
·
Variables should
never have spaces in their names. So
we use timeLeft
instead of time left.
·
If we wanted to
start the timer at a value other than 15, we can do so above.
·
If we want the
time to go faster, we can use a smaller number of steps in the alarm
code. Above, the 30 steps is equal
to 1 second of time since game maker runs at 30 steps per second.
3 –
Creating our font
·
Create
a font (similar to creating a sprite).
·
Name:
ft_countdown
·
Font:
as you wish
·
Size: as you wish
4 – Drawing
our countdown
·
In
the countdown object, add a Draw GUI event (inside Draw).
·
In
that event, add a Set Colour action (draw tab)
·
In
the same event, add a Set Font action (draw tab)
o
Font:
ft_countdown
o
Align:
center
·
In
the same event, add a Draw Variable action (control tab)
o
Variable:
timeLeft
o
X:
room_width/2
o
Y:
room_height/2
5 –
Creating a room to test in
·
Create
a room. You decide on the name and
size.
·
Under
the objects tab, add a countdown object to your room.
Save and test your
game. You should see the countdown
number appear though it won't countdown just yet.
6 – Making
the countdown count down
·
In
the countdown object, add an Alarm 0 event.
·
In
that event, add a Set Variable action (control tab)
o
Variable:
timeLeft
o
Value:
-1
o
Check
relative
·
In
that same event, add a Set Alarm action (main2 tab).
o
Number
of steps: 30
o
Alarm
#: Alarm 0
Save and test your
game. You should now have a
countdown that works. One issue though
is that it counts down into the negatives instead of stopping at zero.
Discussion: In the Alarm 0 event of the countdown
object, we always decrease timeLeft by 1 and reset the Alarm 0 to 30. However, we only want to do this if
timeLeft is not already zero. So lets check for that.
7 –
Stopping the counter
·
Go back to the Alarm 0
event inside the countdown object.
·
Add a Start block action
(control tab)
·
Add a
End block action (control tab)
·
Move the actions so that
the blocks wrap around the other actions.
·
Add a Test Variable action
(control tab)
o Variable:
timeLeft
o Value:
0
o Operation:
Greater Then
·
Drag the Test Variable
action to the top of the actions list (see below).

Save and test your
game. Your countdown should now stop
at zero.
Done!
|