Tutorial 7: Creating Enemy Sprite in GameMaker

Go to the Resources area on the right of your screen:

  1. Right click on Sprites and click Create Sprite
  2. Make sure to name your sprites accurately for future reference

Example: always prefix your sprites with the letters spr

  1. Name your first enemy sprite sprEnemy1

Importing your sprite graphic

In the sprite window:

  1. Click the Import button under where you just typed the name
  2. Browse to where you have saved your sprite GIF files
  3. Click Open and it should import each frame of your animated GIF

Set the sprite origin to centre

Reference the centre point of the enemy sprite as the origin point for its x and y code co-ordinates.

To do this change, Top Left to Middle Centre.

Example: If you have a 64×64 size sprite, the Origin will change to 32×32

Create the enemy object

From the Resources menu:

  1. Right click Object and click Create object
  2. Name the object objEnemy1

– note: You should always prefix your objects with the letters obj for future referencing

  1. Assign it to the sprEnemy1 sprite you just created
  2. Click on the 3 dots in the box beside No Sprite

Make the enemy object move

There are several ways that you can make an object move in the room. Below are some choices. Choose one of these for your first enemy object, feel free to adjust it and then choose a different option for a second enemy object.

Movement option 1: Move to random point in room

  1. Click Add Event button in the Events box and add a Create event
  2. In the description comment change the description to Set up variables.
  3. And add the code:
    flySpeed = 5;//speed of enemy1
    
    move_towards_point(irandom(room_width),room_height,flySpeed);
    
    

The above code will make the enemy fly towards a random point in the room.

Movement option 2: Move enemy towards the player

  1. In a Create Event, instead of the above use the following code.
    flySpeed = max(1, random(2)); //creates a random enemy speed
  2. Add a Step event to the objEnemy1 with the following code:
    if(instance_exists(objPlayer)){
    move_towards_point(objPlayer.x, objPlayer.y-100, flySpeed);
    /*minus 100 from the y-axis 
    stops the enemy from crashing into you*/
    }
    
    

Movement option 3: Move enemy left to right / bounce enemy off room walls

  • In the Step event add this code instead:
    if (median(x,100,room_width-100) != x){
    //in the spawner object you will have to ensure that the lengths are similar to the above
    flySpeed = -flySpeed;
    }
    
    x += flySpeed;
    
  • Remove any code from the Create event that moves the enemy

Important: Destroy the enemy object when it leaves the room

The enemy object still exists as it leaves the room. Therefore, it’s good practice to destroy that object when it leaves the room to free up computer memory. Otherwise, the computer memory will hold that object in it as the game progresses and you will see that the game will become slower over time.

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

  1. Add this code to the Outside Room event:
instance_destroy();
Advertisements
game development and coding courses

Similar Posts

Leave a Reply

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