-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

GameMaker Cookbook
By :

So our player can move and shoot. That's great, but what fun is that when there are no obstacles? Hazards offer a great challenge and can even be the core of your gameplay. How boring would Mario Kart be without banana peels? Or Pitfall without…well, the pits? Video game hazards come in many forms and, if you play games, you've likely encountered most, if not all of them. Spikes, lasers, electrical traps, and falling objects are a few examples, but they have at least one thing in common: if your player character touches them, he/she will be hurt or killed. Now, in most games the latter isn't permanent, what with health bars and lives (we'll get into those later), but it remains a major part of the gameplay. Let's look at creating some simple hazards that can give your players a hard time.
In-game hazards can come in many forms and various visual styles. Like the projectiles we discussed previously they can be static images, but they look much better as animated sprites. We're going to build a spike trap; you can use your own art, but I've included an animated sprite in the downloadable files.
spr_hazard_spike
, and load the necessary images. Before we move, on you're going to need to set up the sprite's collision mask. Sprites are assigned a collision mask by default but you can modify it to your own needs. We only want the trap to be activated and for the player to take damage when the spikes are actually touched, as opposed to the area around the trap.Once you save your selections we're ready to get started.
obj_hazard_spike
and assigning the sprite you made previously.image_speed
to 0
, the other will set trap_set
to 1
.obj_player
as the target.trap_set
is equal to 1
.trap_set
to 0
, and another that sets image_speed
to 1
. Alarm 0
to 60
. Your Actions box should now look like this:Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Alarm 0
.image_speed
to 1
.Alarm 1
to 30
.Alarm 1
, where you'll drag Set Variable
and have it set trap_set
to 1
.image_index
is equal to 4
.alarm[0]
is greater than 0
, as well as another Start and End Block.image_speed
to 0
. When you're done, the Actions box should look like this:image_speed
to 0
.Got all that? It's a lot to do all at once, but we'll go through what it does, step by step. The good news is that you now have a spike trap that springs when touched by the player and stays sprung for a bit before retracting. There's a good reason behind this that we'll explore a little further once we get into player health.
The trap you've just made uses collision detection and alarms to set and reset variables that control the trap's state. On creation, GameMaker sets the image_speed
to 0
, keeping the animation on the 1st frame, and establishes the variable trap_set
, which will only be used by this particular instance of the spike trap. Think of trap_set
as a sort of on/off switch, using binary (0
or 1
) to dictate its current state. When the player collides with the trap, GameMaker checks whether or not the trap is on. If trap_set
is equal to 1
(on), GameMaker proceeds to set image_speed
to 1
(so the animation will begin to play), set Alarm 0
to 60 steps, and reset trap_set
to 0
so that the spike trap will not be activated over and over again. This is to give the player a chance to move away from the trap to avoid being injured a second time. The Step Event checks every single step to see whether or not the sprite's animation has advanced to frame number 4
. If it has, it then checks whether or not Alarm 0
is still counting down. If it is, image_speed
is set to 0
, so that the animation remains on the 4th frame (in the up position) until Alarm 0
is finished counting down. When it has, the image_speed
is set back to 1
, allowing the animation to continue, and Alarm 1
is set to 30 steps. The animation can continue (retracting the spikes) but once it reaches the last frame, the Animation End event then sets image_speed
to 0
again, preventing the animation from looping. The last thing to happen in this long list of events is the end of Alarm 1
, which simply gives trap_set
a value of 1
, thereby resetting the trap to be used again.
It's a lot to take in at once, but creating this trap is a great demonstration of the programming logic we're going to get into later on. For now, enjoy your new spike trap!
Change the font size
Change margin width
Change background colour