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

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:

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:

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.

主站蜘蛛池模板: 额济纳旗| 宜兰县| 山西省| 广水市| 临夏市| 黑山县| 三原县| 类乌齐县| 青神县| 永安市| 博野县| 冕宁县| 改则县| 贵港市| 南汇区| 鄯善县| 遂川县| 岳西县| 鄂尔多斯市| 彩票| 定襄县| 卫辉市| 新兴县| 兴业县| 阳朔县| 福建省| 陇西县| 蛟河市| 桐城市| 洪雅县| 金秀| 潜山县| 梁山县| 长沙县| 田阳县| 博兴县| 淳安县| 巴中市| 克东县| 五寨县| 行唐县|