Enemy Sprite Changes Horizontal Direction
In this tutorial you will learn how to have an enemy change it's sprite depending on what direction it is moving, either left or right.
What you need
Before you start, you will need a basic game with a playable character and
one moving enemy.
You will also need an enemy left and right sprite.
Download a file to work from:
Basic Platform Engine with Enemy
Movement
Enemy Sprite Left / Right
To make your enimies sprite change depending if it's moving left or right, all you have to do, is determine what direction the enemy is moving by checking if the hspeed is greater or less than 0. If it's greater, it is going right, if less, moving left.
This will all be done on an End Step Event, changing sprites is best to be done on the End Step Event, this happens after the Step and Keyboard Events, it is the last Event to happen on a character in each frame.
Checking the Direction and Changing Sprite when Moving
First, you need an End Step event to check the direction and change the sprite on.
| On the Enemy Object, add an End Step Event |
Now we need to check if the character is moving left, we will check this by seeing if the hspeed is less than 0.
| From the Control tab, drag in the IF Variable has Value
question action, the settings will appear. Set Variable as, hspeed Set Operation as, Less Than Set Value as, 0 |
After this IF statement, we know if the enemy is moving left or not, if it is moving left, we want to change the sprite to the left moving sprite.
| From the Main 1 tab, drag in the Change Sprite Action,
the settings will appear. Set Sprite as, the sprite for enemy moving left Set Subimage as, image_index Set Speed as, 1 |
This will change the sprite, the reason we put subimage as one is because, the end step event happens every frame, if we leave subimage as 0, every frame, it will reset the sprite animation back to the first frame, image_index is a variable that remembers what frame the sprite animation is on. Putting this in will continue the animation when we change the sprite.
| From the Control tab, add the Else action. |
The else action will only happen if, hspeed is NOT less than 0, in this case, we want to change the sprite to enemy moving right.
| From the Main 1 tab, drag in the Change Sprite Action,
the settings will appear. Set Sprite as, the sprite for enemy moving right Set Subimage as, image_index Set Speed as, 1 |
That's all you have to do to get the enemy to face left or right depending on which direction it is moving horizontally. If you have more than one direction, you would have to check the direction value, so 0 is right, 90 is up, 180 is left and 270 down. This tutorial just covers horizontal directions.
Checking the Direction and Changing Sprite when Not Moving
You can also make the sprite change if the enemy is not moving, all you have to do is check if the hspeed is zero, if it is, change to a stopped sprite.
Add the following Actions to the TOP of the Actions list on the End Step Event
| From the Control tab, drag in the IF Variable has Value
question action, the settings will appear. Set Variable as, hspeed Set Operation as, Equal to Set Value as, 0 |
| From the Main 1 tab, drag in the Change Sprite Action,
the settings will appear. Set Sprite as, the sprite for enemy not moving Set Subimage as, image_index Set Speed as, 1 |
| From the Control tab, add the Else action. |
| From the Control tab, add the Start Block action. |
Add the following Actions to the BOTTOM of the Actions list on the End Step Event
| From the Control tab, add the End Block action. |
Doing this will only check and change the sprite so it faces left or right if the hspeed is NOT zero.
Download Working Example
enemy_sprite_change_hori.gm6 (click enemy to stop it moving for example)