Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Programming basic enemies

Enemies are more or less an extension of hazards; the basic idea is that if they touch (or shoot a projectile that touches) the player, the player is injured or killed. Only in games, though, not in real life. There is one difference here: enemies move. You can create enemies that will march to their deaths (think Goombas in the Mario Bros games), patrol a given area (think soldiers in the Metal Gear games), or even actively hunt the player (think ghosts in Pac-Man). For now, we're going to take a look at a simple enemy that will patrol a straight line.

Getting ready

As with everything we've done so far in this chapter, you'll need sprites to represent your enemy. You can make your own or you can use the one included in the downloaded files. It's acceptable to use the same sprite for different types of enemies but, from a game design perspective, it's a good idea to differentiate. This will make it easier for your player to identify the enemy types and react accordingly.

Create a sprite and name it spr_enemy_patrol. Load the associated images from the files provided, or create your own, and be sure to set up the collision mask.

How to do it...

  1. Create an object called obj_enemy_patrol and assign the sprite you just made.
  2. Add a Create event and drag and drop Set Variable that sets image_speed to 0.6.
  3. Drag and drop Set Variable, which will set dir as -1, and Transform Sprite, which will change the xscale to -1. Now, that's the simple part. The rest of the actions for (for the moment) will be handled by a Step event.
  4. Add a Step event. This is where you'll need to run several tests in order to dictate your enemy's movement.
  5. Drag and drop Test Variable and have it check whether or not dir is equal to -1.
  6. Beneath this, drag a Start and End Block, within which you'll add a Test Variable that checks whether or not x is greater than 16.
  7. Below this, you'll need a Start and End Block, between which you should place Moved Fixed, and set it to move left at a speed of 1.
  8. Under all of the preceding boxes, drag and drop another Start and End Block.
  9. Within this block, place a Test Variable that checks whether x is less than or equal to 16, followed by another Start and End Block.
  10. Within this block you will need a Set Variable that sets dir to 1, a Transform Sprite that changes xscale to 1, and a Move Fixed that sets the move direction to the right at a speed of 1.

You now have exactly half of the actions required for a patrolling enemy. At this point you can do one of two things: You could copy this entire set of actions, paste the copies directly below the existing actions, and go about changing the variables to their exact opposites, or you could drag and drop new blocks in the same order, but set the variables to their opposites. The former is much faster, but the latter will help you learn more about programming logic. In any respect, your Actions box should look like this:

How to do it...

How it works...

While this Step event contains a lot of actions, it is really doing something quite simple. GameMaker is checking, every game step, for the patrolling enemy's state and position. If headed left and his position on the room's x coordinate is more than 16, he'll continue on in that direction. Once he gets past this point, GameMaker switches his direction, flips the sprite and sends him off to the other side of the room until his x coordinate reaches the room width minus 16 pixels, rinse and repeat.

There's more...

This method of creating a patrolling enemy is a simple enough way to move your enemy in a set space. I chose to have him walk right across the entire room, but you could also have him walk within a smaller space. Another way to accomplish this is to create left and right boxes, remove their visibility by unchecking the Visible box in Object Properties, and placing them at either end of the area you want patrolled. In the enemy's properties, instead of having everything in the Step event, you can control when they change direction by when they collide with the invisible boxes. The same actions would apply; they would just be executed in a different way. This just goes to show you that there are many possible solutions to all of the problems you may encounter when making games; you just have to find them.

See also

Paths will be discussed in Chapter 4, Let's Get Physical – Using GameMaker's Physics System.

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 €18.99/month. Cancel anytime