04 – Platformer Movement Values

Introduction

As of the last tutorial our game has three tile shape collections:

  • SolidCollision – the bricks that make up most of the level
  • IceCollision – floating blocks of ice which should be slippery
  • WaterCollision – an area of the level where the player should be able to swim

This tutorial shows how to create new platformer values for ice and water, and how to change between them depending on collision type.

Creating the Platformer Values

Before we change which platformer values are used based on collision, we first need to define the platformer values for the different movement types. As mentioned before, we will have the following platformer values:

  • Ground – already defined by default
  • Air – already defined by default
  • Ice – will apply when user collides with the ice TileShapeCollection
  • Water – will apply when the user collides with the water TileShapeCollection

To add a new movement value:

  1. Click the Player entity
  2. Click the Entity Input Movement tab
  3. Click the Add Control Values button

Set the following values:

  • Movement Type = Ice
  • Max Speed = 120
  • Speed Up/Down = true (click radio button)
  • Speed Up Time = 1
  • Slow Down Time = 1
  • Jump Speed = 300
  • Hold to Jump Higher = true (click checkbox)
  • Max Jump Hold Time = .2

Repeat the process above to create a water movement. Click Add Control Values again, and set the following values:

  • Movement Type = Water
  • Max Speed = 100
  • Speed Up/Down = true (click radio button)
  • Speed Up Time = 1.3
  • Speed Down Time = 1.3
  • Jump Speed = 120
  • Hold to Jump Higher = true (click checkbox)
  • Max Jump Hold Time = 0.3
  • Gravity = 210
  • Max Falling Speed = 90

We also will modify the existing default values. Modify the Ground values:

  • Max Speed = 200
  • Jump Speed = 300

Modify the Air values:

  • Max Speed = 200

Using Ice Movement

Now that we have Ice movement defined, we can modify our collision relationships to set the ice movement if the player collides with ice. We need to use Ice movement when the Player collides with Ice collision, but the values need to be set back to Ground when the Player collides with regular Solid collision.

To do this:

  1. Expand GameScreen -> Objects -> Collision Relationships
  2. Select the PlayerListVsIceCollision object
  3. Click the Collision tab
  4. Change Ground Movement to Ice

The steps above will set our Player to use Ice movement whenever colliding with the IceCollision TileShapeCollection.

To set the Player to use Ground movement when colliding with our regular SolidCollision:

  1. Select PlayerListVsSolidCollision
  2. Click the Collision tab
  3. Change Ground Movement to Ground

Now our Player will change movement values when moving on ice and solid ground.

Using Water Movement

Next we’ll add water movement to the game. Just like we did earlier with Ice movement, we can set the water movement values when colliding with the Water TileShapeCollection. To do this:

  1. Select PlayerListVsWaterCollision
  2. Click the Collision tab
  3. Change Ground Movement to Water
  4. Change Air Movement to Water
  5. Change After Double Jump to Water

Notice that we set all three movements (ground, air, and after double jump) to Water. The reason for setting Ground and Air to Water is because we want the Player to have the same movement values regardless of whether the Player is on the ground or not.

When the Player leaves the ground (by jumping or falling off of a ledge), it switches from Ground movement to Air movement. If the user presses the jump button while in Air movement, the behavior depends on whether the platformer values have a non-zero Jump Speed.

The Air movement Jump Speed is set to 0, which means that a double jump is not possible.

The Water movement Jump Speed is set to a value greater than 0, so if it is used as Air movement, then a double jump is possible.

In our case, once a double-jump occurs, we remain the Water movement type, allowing jumping over and over. While it might seem weird to think of swimming as “jumping”, the only difference between swimming and jumping is coefficients. We set up our Water movement to have smaller gravity, jump speed, and max falling speed so that it feels more like water.

If we run our game, we can see that the game feels significantly different once the player  jumps in the water.

Unfortunately, once we leave the water we are still using the water collision. This happens because nothing sets the Air Movement back to Air after we leave the water.

Setting Air Movement on No Collision

So far we have set movement values in response to collision. For example, when the Player collides with the Water TileShapeCollection, we set the movement values to Water. We want to set the movement values back to their previous values (Air Movement set to Air, After Double Jump set to null), but we don’t have any shapes to use for setting the values back. If we had a TileShapeCollection for when the user is out of the water, we could create a collision relationship for setting the movement values back.

Fortunately, we don’t have to go through the trouble of creating another TileShapeCollection. Instead, we can create a relationship which is always colliding with the Player. To do this:

  1. Click the PlayerList object in GameScreen
  2. Click the Collision tab
  3. Click the Add button next to <Always Colliding> to create a collision relationship which will always collide

This creates a collision relationship which will perform its collision logic every frame.

We can set the default values that we’d like to be assigned every frame:

Notice that After Double Jump is explicitly set to <NULL> which means the value will be unassigned. Leaving the value as blank is not the same – blank means to leave it as-is rather than to unassign the value. We want this collision relationship to un-assign the Water movement value, so we must set the After Double Jump value to <NULL>.

Finally, we must change the order of the collision relationships.

To do this, right-click on PlayerListAlwaysColliding and select Move to Top.

This moves the PlayerListAlwaysColliding relationship to be first in the list. Normally collision relationship order doesn’t matter, but in this case we want the PlayerListAlwaysColliding to be first in the list, so it is executed first in code. Then if the player does collide with Water, the PlayerListVsWaterCollision can set the  movement values.

Now if we run the game, the Player properly switches between in and out of water movement values.

Conclusion

This concludes the platformer movement value tutorials where we use multiple TileShapeCollections to change the movement values for the player between regular ground/air movement, ice, and water movement. We can turn off the GameplayLayer in Tiled (change its visiblity in the TMX and save it) to see the game with its real art.