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

GameMaker Cookbook
By :

Now that our character can move, logically (or rather, illogically), we want to give him a gun. Though countless games offer projectiles as part of the gameplay, there are a great number of design choices to consider that will ultimately alter the way the player plays. We're going to create simple projectiles right now but later in the book we'll discuss different design choices for creating and controlling gameplay.
spr_projectile
, and load the images you would like to use; you can use a single, static image, but it looks much better if your projectile has a brief animation. (I've provided a projectile animation in the downloaded project files. The sprite is 16px by 16px.)obj_projectile
) and assign the sprite you made.We want to create a projectile at our character's location whenever the player hits the Spacebar. On creation of the projectile, we want GameMaker to verify the direction the player is facing and send it flying in that direction. Before GameMaker can check the direction, though, we need to create a variable that tells GameMaker which direction the object is actually facing. It sounds convoluted but in execution it's really fairly simple, so let's begin.
obj_player
and add a Create event. dir
and set the value to 1
.Keyboard
event for right
, drag and drop Set Variable from the Control tab and have it set dir
to 1
.dir
to -1
.<Space>
. obj_projectile
from the menu but leave the x
and y
values as 0.obj_projectile
and add a Create event.Applies to
select obj_player
and have it test whether or not the variable dir
is equal to a value of 1
.8
.dir
for a value of -1
and have the projectile move to the left. The obj_projectile
properties window should look like this:There's one last thing we should look at, here, and that's how often our player character can shoot. As it is right now he can shoot every time the Spacebar is pressed. Imagine our character is holding a handgun. Now think about how quickly the player could potentially press the Spacebar, over and over. I don't know about you but I imagine a handgun that can shoot that fast must be magical; maybe a wizard made it. There is a way around this magical handgun made by wizards, and that way involves alarms and variables.
obj_player
once more and, in the Create event, drag and drop another Set Variable to the Actions box. shoot
and set the value to 1
.Key Press
event for Space
, drag Set Variable to the Actions box and set shoot to 0
.Alarm 0
to 30
.shoot
back to 1
.shoot
is equal to 1
.shoot
is equal to 1
. To make sure this is the case, drag Start Block from the Control tab and drop it above Create Instance, and drag End Block to the bottom of the list. The Spacebar key press event should now look like this:Congratulations! You now have a player that can shoot projectiles in the direction he is facing and do so at a realistic rate. This outcome is much more appealing than having a player who fires his gun wherever he pleases at incredible rates. You're a loose cannon, player!
In order to make your character shoot projectiles, we've had to use several important elements of programming with GameMaker, namely variables and alarms. When the Spacebar is hit, GameMaker first has to check whether or not the variable shoot
is equal to 1
. If not, nothing happens. You could hit the spacebar as many times as you want, but unless shoot
equals 1
, you'll get nothing out of it. Now, assuming shoot
does equal 1
, GameMaker moves on to create a projectile in the same place as the player, which is (0, 0) relative to wherever the player object is sitting. If you want the projectile to be created near the player, say to give the appearance that the projectile is coming out of a gun, you could change the coordinate values for (x, y).
At the same time, GameMaker is setting shoot
to 0
to prevent the player from shooting again right away, and setting an alarm to 30 steps. At the end of those 30 steps, shoot
is set back to 1
. Each step is the same as one frame in-game, meaning if the game is running at 60 frames per second, the player can fire every half-second.
Once the projectile is created, GameMaker is testing another variable we set: dir
. This is simply to tell GameMaker in which direction the player is facing and GameMaker then sends the projectile off in that direction. Bam. Projectiles. No pun intended.
Projectiles will be expanded on in Chapter 9, Particle Man, Particle Man – Adding Polish to Your Game with Visual Effects and Particles.
Change the font size
Change margin width
Change background colour