Asteroids
2 Firing Bullets

Section Menu

Asteroids Home | 1 | 2 | 3 | 4 | 5

The Firing Mechanism

In the arcade version of the game, the ship can fire bullets. Our first job is to create a sprite called, spr_bullet using the bullet graphic from the resources.

Game Maker Screenshot

Make an object from this called obj_bullet.

Game Maker Screenshot

Return to the ship object window. This is where the firing process starts. Add a Create Event and drag in a Set Variable icon. We will set a variable called canfire to 0 as shown in the screenshot.

Game Maker Screenshot

Go to the End Step event that you created earlier and drag in another Set Variable icon. This time set the value of canfire to relative -1 as shown below.

Game Maker Screenshot

This will be used to time when we can fire the bullets so that there will be a delay between each bullet fired.

Now add a Keyboard event for the CTRL key. Drag in a Test Expression icon and test that canfire<0 as shown,

Game Maker Screenshot

Drag in a Start Block icon followed by a Create Instance icon, filling in the box as shown.

Game Maker Screenshot

Create a sound using the zap sound and call it snd_zap. Drag in the Play Sound icon and play the sound.

Next, drag in a Set Variable icon and set the value of canfire to 15.

Game Maker Screenshot

By adjusting the number, you will be able to increase and decrease the delay between bullets.

Finish with an End Block icon to give you the following,

Game Maker Screenshot

Bullet Motion

Now we must turn our attention to the bullet - open up the object window and add a Create event. Drag in the Move Free icon and fill in the window like so,

Game Maker Screenshot

The bullet will start moving in the direction in which the ship is facing.

Now drag in a Set Alarm icon and set Alarm0 to 60 as shown,

Game Maker Screenshot

This gives you,

Game Maker Screenshot

Now, add an Alarm event for Alarm0 and drag in a Destroy Instance icon.

Game Maker Screenshot

Now we have to deal with what happens when the bullet goes off the screen. Add an Outside Room event and drag in a Wrap Screen icon and fill it in as shown,

Game Maker Screenshot

Save your work and test the bullet firing mechanism. Adjust the speed of the bullet (in its create event) or the delay between bullets (in the CTRL keyboard event) until you are happy with the way it works.

⇐ Previous | Next ⇒