Adding projectiles
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.
Getting ready
- Create a sprite, call it
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.) - Now create an object for the projectile (
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.
How to do it...
- Open the Object Properties for
obj_player
and add a Create event. - Under the Control tab, drag and drop Set Variable to the Actions box.
- Name the variable
dir
and set the value to1
. - In the
Keyboard
event forright
, drag and drop Set Variable from the Control tab and have it setdir
to1
. - Do the same for the left keyboard event but have it set
dir
to-1
. - Now create a Key Press event and select
<Space>
. - Under the Main1 tab, drag and drop Create instance to the Actions box.
- Select
obj_projectile
from the menu but leave thex
andy
values as 0. - Open the Object Properties for
obj_projectile
and add a Create event. - Under the Control tab, drag and drop Test Variable to the Actions box.
- Under
Applies to
selectobj_player
and have it test whether or not the variabledir
is equal to a value of1
. - Now, under the Move tab, drag and drop Move Fixed to the Actions box and have our projectile move to the right at a speed of
8
. - In the same Create event, repeat these steps but check
dir
for a value of-1
and have the projectile move to the left. Theobj_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.
- Open up the properties for
obj_player
once more and, in the Create event, drag and drop another Set Variable to the Actions box. - Call the variable
shoot
and set the value to1
. - In the
Key Press
event forSpace
, drag Set Variable to the Actions box and set shoot to0
. - Under the Main2 tab, drag Set Alarm to the Actions box and set
Alarm 0
to30
. - Now click on Add Event, select Alarm 0, and drag Set Variable to the Actions box and have it set
shoot
back to1
. - There's just one final change to make, now. Go back to the Space key press event and drag Test Variable to the Actions box and have it test if
shoot
is equal to1
. - Make sure this is the first thing GameMaker does when Spacebar is pressed by dragging it to the top of the list.
- Now, the rest of the actions here should only be carried out if
shoot
is equal to1
. 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!
How it works...
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.
See also
Projectiles will be expanded on in Chapter 9, Particle Man, Particle Man – Adding Polish to Your Game with Visual Effects and Particles.