Shooting monsters

Destroy Bullet, kill monsters

Obj: bullet

First we'll create a bullet object

We'll set it up so it'll be destroyed if it hits a wall or goes outside the room. 

  • Collision with block
    • Destroy Instance
  • Outside Room 
    • Destroy Instance

We'll also set it to kill the monsters when the bullet hits it. Create the Collision with one of the monsters:

  • Collision with monster
    • Play Sound (kill monster)
    • Set Score: give player 50 points
    • Destroy Instance (other = monster)
    • Destroy Instance (self = bullet)

Then duplicate it for the flying monster...


getting ammo

To make things a bit more interesting, the player first needs to find some ammunition to be able to shoot. To this end we introduce a variable we call ammo that indicates how much ammunition the player has - it has to be this name, it can't be the same name as the object/sprite so ammunition won't work.

Obj: ammunition

Create the ammunition object: has a simple sprite and does nothing. It just waits to be picked up by the player. Give it a depth of 10.

Obj: character

Go into your character object and add a Create event.

  • Set Variable: variable = "ammo" and value = "0" to start.
    The player can't shoot until they've picked up the gun/ammunition 

Then add a Collision with ammunition event.

  • Set Variable: pick up the gun = set variable "ammo" = 10, relative
  • Destroy Instance: other (=gun, not character!)


Shooting bullets

Obj: character

Add a <Space> Key Press (not Key Board) and make it so every time the player presses the Space Bar, they can shoot. 

  • Test Variable: is variable "ammo" > 0?

Add Start & End blocks...

We have to do some testing - which direction is the character facing? That's the direction we want to shoot the bullets...

  • Inside those blocks, start adding actions::
    • Test Variablecheck if our character is facing left
      variable "sprite_index" = value "characterL"?
  • Create Moving: then we shoot a bullet to the left
    object "bullet", x=0, y=0, speed=12, direction=180, and relative
  • Else
  • Create Moving: otherwise shoot bullet to the right; can duplicate other one, only difference is the direction
    object "bullet", x=0, y=0, speed=12, direction=0, relative
  • Set Variable: once it shoots, take away a bit of ammo
    set variable "ammo" to "-1", relative

It should end up looking like this...

Put the gun in the room near the character and test it out - if you pick it up, can you shoot to the right and kill the monster? does it disappear? can you shoot to the left?