Shooting Bullets

Bullet moving, destroy itself

Obj: bullet

Create a bullet object. We will give it the default depth of "0" such that it will appear below the plane but above the islands.

Add a Create event....

When the bullet is created, we want it to move upwards so Set the vertical speed to "-8".

Next we add a Outside Room event. We have to destroy it once it leaves the room or else you'll have more and more bullets flying around. 

  • Destroy Instance: then destroys itself

Player can shoot

Obj: myplane

Want the bullet to appear when the player (who controls the main plane) hits the space bar. Go into your myplane object and add a Space Bar event.

  • Create Instance: we create a bullet just in front of the plane (0,-16). Make sure its relative so it's just in front of the plane (not top left corner of the room!)

If you test it out, you'll see something like this...


Bullets spaced out

Obj: myplane

But we don't want too many bullets at the same time. So we only let the player fire 2 bullets every second, that is one bullet every 15 steps.  Add a Create event...

    • Set Variable: set the variable can_shoot to "1"

Go into the <Space> event you already have. We'll first test if we can shoot, and if so, we'll create a bullet, and then set a timer/alarm... 

  • First we check, are we allowed to shoot? (= if "can_shoot" true/1).
    Test Variable: if can_shoot = 1

If so, then inside Start & End blocks, keep the Create Instance action you already have as well as:

    • Set Variable:  set the variable can_shoot to 0 (false, indicating we can no longer shoot).
  • Set Alarm0: to 15. The alarm ticks down to 0 with one tick each step. You can change the speed with which you can shoot by changing the value of the alarm clock.

You will also need an event Alarm0 - what happens then the alarm reaches 0, runs out of time? In this situation, we want to set the variable can_shoot back to true/1 so the plane can shoot again.

Now if you test it in your room, the bullets should be spaced out...