03. Controlling a Platformer Entity


This tutorial covers how to read input to move a platformer entity. We will also be creating basic collision to prevent our entity from falling off of the screen when the game runs.

Adding Collision

Collision for platformers may be defined using individual shapes (as we will do in this tutorial) or may be defined in a TileShapeCollection, which supports collision defined in the Tiled program.

For simplicity we will define a single collision rectangle to use in our project:

  1. Expand GameScreen in Glue
  2. Right-click on Objects
  3. Select Add Object
  4. Select AxisAlignedRectangle
  5. Click OK
  6. Select the newly-created AxisAlilgnedRectangleInstance
  7. Set Y to -300 and Width to 2000 and size values as shown in the following image:

Adding Collision in Code

Now that we’ve added a rectangle to our GameScreen we need to write code so that the MainCharacterInstance collides against it:

  1. Open the project in Visual Studio
  2. Navigate to GameScreen.cs
  3. Find the CustomActivity  method
  4. Modify the method so it includes the following code for collision:

CollideAgainst vs CollideAgainstMove and CollideAgainstBounce

Note that the code above uses CollideAgainst , which is a function provided specifically by the platformer plugin. This collision method includes all of the logic necessary to perform proper platformer movement (including jumping). The other methods typically used in collision ( CollideAgainstMove  and CollideAgainstBounce ) may appear to work because they will prevent the platformer from falling through the ground, but the platformer entity will not be able to jump on surfaces which have been collided against using the Move and Bounce variants.

In short, it’s best to always use CollideAgainst  on a platformer entity.

Now the character will land on the rectangle instead of falling off screen.

[+]Additional Information - Colliding against Tiled Maps

This tutorial performs collision between the character and a single AxisAlignedRectangle ; however, the platformer plugin also includes support for colliding against  TileShapeCollection , which is commonly used to represent collision on tilemap environments.

For information on creating a TileShapeCollection for your tile map, see the TileMap Adding Collision tutorial.

Once you have a TileShapeCollection created, you can collide against it using the following code:

Controlling the Entity with Input

Platformer entities require input to move horizontally and to jump. Input can be assigned in the screen containing the entity or in the entity’s custom code. To assign input in the scree:

  1. Go to GameScreen.cs
  2. Modify the CustomInitialize  function to contain the following input assignment code:
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.


Now our platformer character reacts to input and collides with the environment (which for us is a single AxisAlignedRectangle). The next tutorial takes a deeper look at the control values.