Create a Playable Character
This part of the tutorial explains how to add a playable character to the game.
You'll learn how to:
- Create an object that represents the playable character.
- Let the player control the object using the arrow keys.
- Follow the playable character with the camera.
Series
You are reading Part 2 of the Platformer Tutorial.
- Part 1: Getting started
- Part 2: Create a Playable Character
- Part 3: Create Platforms
- Part 4: Animate the Playable Character
- Part 5: Add Collectible Coins to the Game
- Part 6: Add Enemies to the Game
- Part 7: Make Enemies Kill the Player (and the Player Kill the Enemies)
- Part 8: Add Checkpoints to the Game
Step 1: Create an object
The first step is to create an object that represents the playable character. If you followed the first part of this tutorial, this should be a familiar process.
To create an object for the playable character:
- Create a Sprite object named "Player".
- Use the "p1_stand.png" asset as the animation's only frame.
- Assign a name of "Idle" to the animation.
- Drag an instance of the object into the scene.
Note
Part 4: Animate the Playable Character explains how to animate the character.
Step 2: Add a behavior to the object
If you preview the game, you can't control the "Player" object. This is because, by default, objects don't do anything. To make objects do something, you need to assign one or more behaviors to them.
GDevelop comes with a large library of behaviors that you can add to objects in a matter of clicks. One of these behaviors is the Platformer character behavior, which lets you control an object as a playable character.
To add the Platformer character behavior to the object:
- Right click the Player object.
- Select Edit object.
- Switch to the Behaviors tab.
- Click Add a behavior to the object.
- Select Platformer character.
- Click Apply.
If you preview the game, you can control the player with the arrow keys. There's a side-effect though: an object with the Platformer character behavior is affected by gravity, so in this case, the playable character quickly fall out of the frame.
Step 3: Follow an object with the camera
To prevent the character from falling out of frame, the game needs platforms and a camera that follows the player. The next part of the tutorial is about platforms, so this section is about the camera.
To make the camera follow the player, you need to create an event. In GDevelop, events let you define cause and effect, so when something happens (cause) you can trigger something else to happen (effect).
Events are made up of two parts: conditions and actions. The condition is what determines when the event happens and the action is what happens when the event occurs.
GDevelop has a large library of conditions and actions that add all sorts of features to a game. A big part of learning GDevelop is discovering the available options. In this case, the game needs the Center the camera on an object action.
To add the Center the camera on an object action:
- Opens the Events editor.
- Click Add a new event.
- Without specifying a condition, click Add action. By not specifying a condition, the action runs in every frame. Therefore, if the game runs at 60 frames per second, the action runs 60 times per second.
- Click Other actions.
- Expand Layers and cameras.
- Select Center the camera on an object.
- Select the "Player" object.
- Click OK.
If you preview the game, the player still falls -- there's still no platforms, after all -- but the camera also follows the player. As a result, the player remains within the frame.