Creating Enemy Sprite with movement
Tutorial 7: Creating Enemy Sprite in Game Maker Studio 2
Go to the Resources area on the right of your screen:
- Right click on Sprites and click Create Sprite
- Make sure to name your sprites accurately for future reference
Example: always prefix your sprites with the letters spr
- Name your first enemy sprite sprEnemy1
Importing your sprite graphic
In the sprite window:
- Click the Import button under where you just typed the name
- Browse to where you have saved your sprite GIF files
- 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:
- Right click Object and click Create object
- Name the object objEnemy1
– note: You should always prefix your objects with the letters obj for future referencing
- Assign it to the sprEnemy1 sprite you just created
- 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
- Click Add Event button in the Events box and add a Create event
- In the description comment change the description to Set up variables.
- 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
- In a Create Event, instead of the above use the following code.
flySpeed = max(1, random(2)); //creates a random enemy speed
- 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.
- Add the following event to objEnemy1 – Other – Outside Room
- Add this code to the Outside Room event:
instance_destroy();