官术网_书友最值得收藏!

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 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.

主站蜘蛛池模板: 同德县| 合江县| 尤溪县| 县级市| 南雄市| 黎城县| 九江市| 石渠县| 无极县| 南通市| 澄迈县| 喀喇沁旗| 方城县| 神木县| 新绛县| 阿克苏市| 肇州县| 宣汉县| 库车县| 正宁县| 铜梁县| 乌苏市| 册亨县| 阿鲁科尔沁旗| 炉霍县| 黄冈市| 宾阳县| 莆田市| 阜平县| 渭源县| 界首市| 长乐市| 时尚| 全州县| 浑源县| 叶城县| 姜堰市| 香港| 临沧市| 章丘市| 任丘市|