03. Controlling a Platformer Entity

Introduction

This tutorial covers how to read input to move a platformer entity. We will also be creating a level to test out our platformer entity.

To create a level and collision, we will be using a Tiled level. For more detail on how to create Tiled levels, see the Tiled tutorial.

Adding a Level

Next we’ll add a level. To add a level:

  1. Click on the Quick Actions tab in Glue
  2. Click Add Screen/Level
  3. Enter the name Level1
  4. Check the Add Standard TMX File option
  5. Click OK

Glue will ask you to name the new map. Enter the name Level1Map and click OK.

Now you should have a new screen called Level1 in Glue.

Finally we’ll add collision to our level for the MainCharacter to walk on. To do this:

  1. Open Level1Map in Tiled

  2. Add collision using the top-left tile

Now if we run the game, we have our level set up, but the player will still fall through the level. Next we’ll add a CollisionRelationship between the player and the solid collision.

Adding a Collision Relationship

Currently our game has collision defined through the Level1Map, but we haven’t told our game that the MainCharacterInstance should collide against it. To do this:

  1. Expand GameScreen Objects
  2. Drag+drop the MainCharacterList onto the SoundCollision to create a relationship
  3. Mark the newly-created CollisionRelationship as Platformer Solid Collision

Now the player will collide with the level.

Controlling the Entity with Input

By default the platformer entity already supports a default set of controls. To see this, select the MainCharacter, then select the Entity Input Movement tab.

By default the platformer will be controllable with a plugged-in Xbox Gamepad. If no Gamepad is detected, then the entity can be controlled with WASD and Space.

If you want to override this functionality, you can change the controls. These controls can be changed in the MovementInput code (to apply to all players) or in the GameScreen. We recommend making the changes in GameScreen so that different characters can have different input in a mulit-player game.

To change the character to jump with the Enter key and to move with the arrow keys:

  1. Go to GameScreen.cs
  2. Modify the CustomInitialize  function to contain the following input assignment code:
void CustomInitialize()
{

    MainCharacter1.JumpInput = InputManager.Keyboard.GetKey(Microsoft.Xna.Framework.Input.Keys.Enter);

    MainCharacter1.HorizontalInput = InputManager.Keyboard.Get1DInput(
        Microsoft.Xna.Framework.Input.Keys.Left, Microsoft.Xna.Framework.Input.Keys.Right);
}

The code above added keyboard controls so that the main character can be moved horizontally with the A and D keys and jumps using the space bar.

Conclusion

Now our platformer character reacts to input and collides with the environment. We can change the environment by opening Level1Map at any time and painting new tiles. The next tutorial takes a deeper look at the control values.