This recipe explains how to set up behaviors for a simple platformer character
that can be controlled by the keyboard to move and jump.
Click here for a completed example
Setting the Stage
First, we need to make sure that the game has some gravity, and that the gravity is pulling the expected direction.
Open up the Game Settings panel by clicking the Settings button on the bottom toolbar. Set the gravity "x" (horizontal)
direction to "0", and set the gravity "y" (vertical) direction to 16.
A Simple Environment
Next, we need to ensure that this level has some ground for the player to run and jump on.
Click on the bottom corner to create a new game object. Set the
"type" to "Ground", and set the "friction" to "100". Make sure that "is solid" is selected, but "movable" is not.
Click your new ground object and select "clone", then click the level to fill out the ground. Feel free to create
some platforms as well.
Adding the Player
Finally, we need a player object, so click anywhere on the level and select "create". Make sure that "movable" and "is solid"
are both selected.
Note: The player object must be set to "movable" so that we can move it
around the screen, and it needs to be set to "solid" so that it will collide with the ground, not pass through it.
Opening the Behavior Editor
To start adding behaviors to your player object, click on the object you just created to use as your player and
select "Edit" to open the object's edit panel.
Click on the "Behavior" button to open the behavior editor.
On the left side of the editor is a menu of behaviors, grouped by category. To add a new behavior to your object
just click one of the behavior buttons, and the behavior will appear in the editor window. The new behaviors can
be arranged by dragging them.
Clicking on a behavior will open its properties panel, where you can edit its associated properties. The bottom
of the properties panel also has a "Delete" button, which will remove this behavior and any of its links.
Reacting to the Keyboard
We would like the player to move left and right when the left and right keys are pressed, so we will start by
adding some keyboard triggers. Under the "Triggers" category, click the "keyboard" button, which will add a
new keyboard trigger.
Assigning a Key
Click the keyboard behavior you just created, which will open up its properties panel. When you click the "Change Key"
button, the behavior will be listening to a new keyboard key, and its label will say "Press any Key".
Click the right arrow on your keyboard, and the "current key" should change to "Right". Check the "repeating" box, so that
holding the key down will continue to trigger this behavior.
Next add another keyboard trigger, but this time assign it to the left arrow key.
Making it Move
There are multiple ways to move an object, but to make the player react instantly when you press the key, we will update
the object's velocity directly. Under the "properties" category, click the "Velocity" behavior button which will add a
Updating the Velocity
To update our object's velocity, we need to send a number value to one of it's inputs. For left and right movement, we will
only be concerned with the "X" or horizontal velocity. We would like to set the X velocity to 6 when we press the right arrow
key, and set it to -6 when we press the left arrow key.
Add two "Number" behaviors, which can be found under the "Logic & Math" category. Their values can be changed in their property panels,
which you can open by clicking on them. Set one number to 6 and the other to -6
Linking it Together
Right now, all the behaviors are in place, but they are not yet linked together. First click the "Right" keyboard behavior's "down"
output port and drag it to the "Number" behaviors "in" input port. When the link turns green it is connected. If you make a mistake
when connecting nodes, just click the link to delete it and try again. Next connect the "Number" behavior's output port to the Velocity's
Now, when the game is running and the Right arrow key is pressed, the value 6 will be sent to our object's X velocity causing it to move
to the right. Link up the Left keyboard trigger the same way, except using the value -6.
Test your progress by pressing the Escape key on your keyboard, or clicking the "Play" button on the bottom toolbar. The player object
should now move left and right when using the arrow keys.
In order to add jumping, we will use a different method to move the object. Instead of directly updating the velocity of the object,
we will add an upward force to give the jump a more natural feel.
Add a keyboard trigger as before, this time assigning it the Up arrow key. We will also need a Number, set to the value 6.
Finally, add an Impulse behavior from the components category. Link the behaviors so that pressing the Up arrow
will send the value 6 into the "Y" input of the Impulse component.
Test the game again to verify that it all works correctly, and feel free to experiment. Here are some things you may want to try:
- Increase or decrease the number values for movement or jumping
- Adjust the gravity
- Clone more blocks around the level to stop the player from leaving