Step 3

Updating the facing variable

Next let’s work on coding our game to correctly update the position of our player.

Create a new function, call it “changeFacing” and and the code as shown.

Here we are assigning a different value for each direction and updating the facing variable when a different direction button is pressed.

Calling the changeFacing function

Add the code as shown so our game is constantly calling the changeFacing function and updating the facing variable.

Adding walking animations

Next, let’s add some walking animations to our character.

Add the code as shown and create/add a walking animation for each of the four directions, set the interval to 150.

Stopping the walking animations

Next, let’s add a way to tell our animations to stop.

Add the code as shown to set a sprite to appear when the player lets go of any of the four directional buttons.

Testing the code/animations

Now with all of that completed, go ahead and test it out!

Our player should now be able to walk in all 4 directions with 4 different animations!