|
Alarms |
|
Alarms are something nearly every good game needs, anything that needs timing, needs an alarm (you can use timelines and these are discussed in a future Timeline Tutorial). You may find the concept of alarms confusing to grasp at first, but once you understand, you'll wonder how you ever did without them!
The countdown begins! If you think of an alarm as a countdown timer, you're half way to understanding them, once an alarm is called from an event, the countdown begins! Take the following call to an alarm for example:
alarm[0] = 90;
What happens when the game comes across an alarm call, like the one above, is a countdown begins from the number after the equals sign, when it reaches 0, the specified alarm event is called. (There are 12 alarm events that can be called per object, these are numbered [0] - [11]).
As mentioned, the countdown starts from the number after the equals sign, in the above example, 90, but what exactly does that number represent? You may think it's the number of seconds to countdown before calling the alarm event, but it isn't, it's the number of STEPS.
STEPS? When your game runs, it runs in STEPS (what's called Frames Per Second, or FPS). The default for a Game Maker game is 30 steps per second and can be found in the Room Properties. (Each room can have a different speed if need be):
To calculate the number of steps in the number of seconds you want, you do this:
Therefore, taking the number of steps per second into account, the 90 in the above example translates to 3 seconds, like so:
--oOo--
How do you use them? Hopefully, you have grasped the basic concept of alarms but how do you actually use them? Well, one example would be in a platform game where a cannon, on one side of the screen, fires a cannonball across to the other side after 5 seconds, but how?
In the Create Event for the cannon object, you could have something like:
And here's the GML version:
So as soon as the cannon object is created, it will do the various things then hit the alarm event: Set Alarm 0 to 150, starting the countdown in steps. [Remember it's SECONDS x STEPS: 5 x 30 = 150].
You would also create an alarm[0] event and place the things in there you wish to happen when it's called, in this case, after 5 seconds:
You'll notice in the alarm[0] event above, there is another alarm call at the end, and this alarm call is calling itself! This is useful for looping alarms and in the above example, a cannonball will shoot across the screen every 5 seconds as the alarm event is resetting the countdown to recall itself again.
Here's the GML version:
Revision #3 |