Game Development Reference
Firing bullets in four directions
In the project files, you'll find two classes: Sa]lkj[Bkqn@ena_pekjo and >qhhap[Bkqn@ena_pekjo. You
can use them so that the player can shoot bullets in four directions. Here's how to make them work:
1. Open the `qjcakjI]va*bh] file.
2. Select the Sa]lkj symbol in the Library and open its Symbol Properties window.
3. Enter Weapon_FourDirections in the Class field and click the OK button.
4. Select the >qhhap symbol in the Library and open its Symbol Properties window.
5. Enter Bullet_FourDirections in the Class field and click the OK button.
6. Save the `qjcakjI]va*bh] file.
7. Test the project. You can now fire bullets in any of four directions, depending on which arrow
key you press. Figure 8-29 illustrates this.
Figure 8-29. Use the arrow keys to shoot bullets in four directions.
The code is provided as follows, and I'll go into some aspects of it in a bit of detail, but here's an
overview of how it works.
The Sa]lkj[Bkqn@ena_pekjo class has a new property called [`ena_pekj that is assigned the value
ql, `ksj, habp, or necdp, depending on which arrow key is pressed. The bullets need to know
which direction they need to travel, so the [`ena_pekj property is passed to them directly as a param-
eter when they're created. (Have a quick look back at Chapters 3 and 7 if you're a bit hazy on what
method parameters are and how they work). The bullets then use that direction to assign the correct
horizontal and vertical velocity. The two classes work together to produce the effect you see on the
stage. Figure 8-30 illustrates the process, and I'll explain it in detail ahead.