Character Direction Face

Before you start, you will need either a Maze or Platform engine character.
You will also need at least two direction sprites, left and right.

Download the Basic Movement engine here

 

To change the way your character faces, all you need to do is change the sprite when you change direction, that is it.

The action to change a sprite on an object is this: Icon Change Sprite

This action will change the sprite on the object to another, you can say what frame of the animation the new sprite should first show and how fast it should animate.

 

Now, we will make the character's sprite change when it moves left or right.

On the character's Left Keyboard Event,

From the main1 tab, drag in the Change Sprite action.Icon Change Sprite

The settings for this action will pop up.

Settings Change Sprite

First, you must choose the new sprite you want to change too.

You must then choose the subimage, a subimage is the frame in the animated sprite.
Below is an example of the pacman left animation,

Example Image Index

By deafult the subimage is set as 0, this will show the first frame in the animation when you change the sprite. If you do this, everytime the change sprite action happens, it will put the animation back to the first frame. Because the Keyboard event happens 30 times a second, the sprite will change 30 times a second and always go to the first subimage.

To solve this problem, we can set the subimage as the current subimage so it continues the animation. The current subimage of an object's sprite is remembered in a variable called, image_index

Type, image_index in the subimage box, this will use the value from the image_index variable as the subimage when changing sprite.

The speed can go from 0 to 1, 0 being no animation and 1 being normal speed, if you want half speed, put 0.5.

Leave the image speed as 1.

Click ok.

 

Repeat this putting the Change Sprite Action on the Right Keyboard event, this time, changing the sprite to pacman right.

After you have done this test your game.

When you press left or right, the sprite will now change, when you stop moving, your character is still animating. We can stop this on the No-Key Keyboard Event. All we have to do is change the sprite again to the pacman stand sprite.

If you want to stop the animation on it's current frame, all you have to do is change the animation speed on the No-Key Keyboard Event.

To change the animation speed, on the No-Key Keyboard Event

Use the Set Variable Action from the Control Tab Icon Set Var

When you drag the Set Variable Action onto the Event, the Settings will appear,

Settings Set Variable

The variable that remembers the speed of the animation is called, image_speed

Type this in the Variable Name box and put the Value as 0

This will change the image_speed on the object to 0, stopping the sprite animating.

 

Download working example

sprite_change_direction.gm6

 

Still Having Problems?

My character gets stuck in the wall when i'm using the sprite change, what's wrong?

This could be a number of things,

First, check that your character collides with the wall properly, the collision event with the wall should look like this,

Example Collision Event Wall

The settings for Move to Contact Position should be,

direction: direction
maximum: 12
against: solid objects

Only set vertical speed back to 0 if you are using a gravity on your character.

Still getting stuck? Maybe your sprite is getting stuck in the wall.

In the Sprite Properties, turn off Precise Collision Checking, you have to be in Advance Mode to change this setting.
Turn off Precise Collision Checking for ALL of your charater sprites.

Example Precise Collision Checking Off

Still getting stuck?

Make sure that all of your sprites for the character are EXACTLY the same size.

What? Still getting stuck!?

Make sure the Origin is the same on all of your character sprites.

Those are the main reasons why a sprite would get stuck in the wall and those are the steps to prevent it. If you're still having problems with getting stuck in a wall, get in contact.