Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
GameMaker Cookbook

You're reading from   GameMaker Cookbook Over 50 hands-on recipes to help you build exhilarating games using the robust GameMaker system

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781784399849
Length 212 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Game Plan – Creating Basic Gameplay FREE CHAPTER 2. It's Under Control – Exploring Various Control Schemes 3. Let's Move It – Advanced Movement and Layout 4. Let's Get Physical – Using GameMaker's Physics System 5. Now Hear This! – Music and Sound Effects 6. It's All GUI! - Creating Graphical User Interface and Menus 7. Saving the Day – Saving Game Data 8. Light 'em up! – Enhancing Your Game with Lighting Techniques 9. Particle Man, Particle Man – Adding Polish to Your Game with Visual Effects and Particles 10. Hello, World – Creating New Dimensions of Play Through Networking Index

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

  1. 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.)
  2. 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...

  1. Open the Object Properties for obj_player and add a Create event.
  2. Under the Control tab, drag and drop Set Variable to the Actions box.
  3. Name the variable dir and set the value to 1.
  4. In the Keyboard event for right, drag and drop Set Variable from the Control tab and have it set dir to 1.
  5. Do the same for the left keyboard event but have it set dir to -1.
  6. Now create a Key Press event and select <Space>.
  7. Under the Main1 tab, drag and drop Create instance to the Actions box.
  8. Select obj_projectile from the menu but leave the x and y values as 0.
  9. Open the Object Properties for obj_projectile and add a Create event.
  10. Under the Control tab, drag and drop Test Variable to the Actions box.
  11. Under Applies to select obj_player and have it test whether or not the variable dir is equal to a value of 1.
  12. 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.
  13. In the same Create event, repeat these steps but check dir for a value of -1 and have the projectile move to the left. The obj_projectile properties window should look like this:
    How to do it...

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.

  1. Open up the properties for obj_player once more and, in the Create event, drag and drop another Set Variable to the Actions box.
  2. Call the variable shoot and set the value to 1.
  3. In the Key Press event for Space, drag Set Variable to the Actions box and set shoot to 0.
  4. Under the Main2 tab, drag Set Alarm to the Actions box and set Alarm 0 to 30.
  5. Now click on Add Event, select Alarm 0, and drag Set Variable to the Actions box and have it set shoot back to 1.
  6. 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 to 1.
  7. Make sure this is the first thing GameMaker does when Spacebar is pressed by dragging it to the top of the list.
  8. Now, the rest of the actions here should only be carried out if 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:
    How to do it...

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.

You have been reading a chapter from
GameMaker Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781784399849
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image