Tutorial 8: Creating Enemy Lazer / Bullet

Go to the Assets Browser resources area menu:

  1. Right click on Sprites and click Create Sprite or you can click on Resources menu at the top of GameMaker studio.
  2. Make sure you name your sprites correctly – always prefix your sprites with the letter spr
  3. Name the sprite sprEnemyLazer
  4. Click the Import button
  5. Browse to where you have saved the enemy lazer or bullet graphic
  6. Set the Origin Point to Top Centre

Why? You want the enemy lazer to start at the top point, if the enemy is shooting down towards you.

Create the enemy lazer / bullet object

Under Resources:

  1. Right click Object and click Create object
  2. Name the object objEnemyLazer
  3. Assign sprEnemyLazer sprite you just created by clicking the 3 dots in the box beside No Sprite

Make the enemy lazer move

  1. Click Add Event button in the Events box and add a Create event
  2. In the description comment change the description to Enemy Lazer Variables.
  3. And add the code:
flySpeed = 15; //speed of enemy lazer

Destroy the enemy lazer object when it leaves the room

The enemy lazer object still exists as it leaves the room. Therefore, it is good practice to destroy that object when it leaves the room to free up computer memory.

  1. Add the following event to objEnemyLazer – Other – Outside Room

Add this code to the Outside Room event:

instance_destroy();

 

Make the enemy lazer / bullet move down the screen

  1. Add a Step Event to your lazer or bullet object
  2. Add the following code to the step event:
y += flySpeed; 

//moves objEnemyLazer down the room. 

//This would change to x if the enemy was shooting across the room towards the player object

 

Add an alarm event to the Enemy object

What is an alarm? An alarm event is an event that must be triggered. You tell an alarm to go off initially in a Create or Step event. We tell the Enemy Object to shoot the lazer.

  1. Open the enemy object  objEnemy1
  2. Add an Alarm Event to it – Alarm 0
  3. Add the following code to the alarm event
instance_create_layer(x,y,layer,objEnemyLazer);

Note: You could also add a sound here for the enemy lazer. Go back to how to the adding sounds tutorial, but put the code to create the sound into the enemy Alarm event

How often do you want your enemy to fire?

  1. In the Create Event for enemy objEnemy1 , add the following code to randomise the setup times for the alarm events:
fireRateLow = 20; //2 frames
fireRateHigh = 60; //6 frames


alarm[0]  = 120; //first time the alarm is triggered will be 120 frames into the game

 

  1. In the Alarm Event of objEnemy1, reset the alarm to tell the alarm when to trigger again.
    Add this code to the enemy alarm event:
alarm[0]  = irandom_range(fireRateLow, fireRateHigh); 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *