← All templates
Platformer
IntermediateTemplateWeb & desktop

Platformer

Game · Platformer

Run, jump, grab six coins and reach the flag. Built from components: a character controller, solid platforms, pickups and trigger zones, with a state graph that turns the hero left and right and plays run and jump animations.

by Arcadia Studio
Read the guide ↓

Platformer

Run and jump across the platforms, grab the six coins, and reach the flag. Fall into the pit and it's Oops!, with a button to try again.

Almost everything here comes from components, the ready-made cards in Properties:

  • The hero gets a Character controller.
  • The ground and platforms are Static bodies.
  • The coins are Pickups.
  • The flag and the pit are Trigger zones.

A state graph picks the hero's animation: standing, running or jumping, facing left or right. The only code is the small scripts the components write for you, and you'll change a couple of lines in two of them.

The level

What you'll learn

  • Giving controls behaviour with components, and tuning them.
  • Reading and changing the script a component writes.
  • Making a sprite's animations with clips, and letting a state graph switch between them.
  • Using trigger zones to open another screen when the player reaches them.
  • Showing a score that counts itself, with ${score}.

Glossary

WordWhat it means
ComponentA card in Properties that adds behaviour to a control, like a Character controller or a Pickup. Some write a small script for you.
Physics bodyA control the physics engine handles. Dynamic bodies fall and bump into things. Static bodies never move, so everything stands on them.
GravityHow strongly dynamic bodies fall, set per screen (900 here).
TriggerA body that things pass through. It fires trigger_enter when something enters it.
SpriteA control that plays frames from a picture sheet.
ClipA named run of a sprite's frames, like run_right.
State graphA list of states (each plays a clip) and the ways out of each state (when to switch). It runs by itself.
input:rightIn a state graph, true while the right input is held. !input:right means it isn't held.

Rebuild it from scratch

1. Project and screens

  1. File → New project, named Platformer with the Id platformer. In **Project → Project settings…, set Made for to Web & desktop only**.
  2. Make four screens, all 320 × 180:
    • title (tick Main screen)
    • level
    • win
    • gameover
  3. Build title, win and gameover like the Beginner templates: a background, labels, and buttons whose click actions are play_sound and open_ui. Play, Play again and Try again open level, and Menu opens title.
TitleYou made it!Oops!
TitleWinGame over

2. The level screen

  1. Select level and click Screen settings.
  2. Set Gravity to 900 and Tick interval (ms) to 16. The game checks the controls every 16 ms.
  3. Add two variables: score=0;air=no.

Add a light-blue Panel as the sky, filling the screen.

3. Ground and platforms

  1. Add Panels for the ground and platforms. Brown fill, a green Border 3 px wide, and **Corners → Radius** 3. The template uses:
    • ground_left: 0, 152, 130 × 28
    • ground_right: 170, 152, 150 × 28. The gap between them is the pit.
    • platform1 to platform4: four 44 × 10 platforms at different heights.
  2. For each one, open Properties → Advanced · web & desktop, click + Add component, and choose Static body. Now it's solid.

4. The hero

  1. Draw the hero in the Pixel editor as four 16 × 16 frames, all side views: facing left, left stepping, facing right, right stepping.

The hero's four frames

  1. Add a Sprite called hero on the left of the ground.
  2. Open the Sprite sheet editor (double-click the sprite), set the frame size to 16 × 16, and make six clips:
ClipFramesSpeed
idle_left01
run_left0, 18
jump_left11
idle_right21
run_right2, 38
jump_right31
  1. With the hero selected, + Add component → Character controller. That does three things:
    • It makes the hero a dynamic body.
    • It adds the inputs left, right and jump (arrows, A/D, Space/W/Up).
    • It writes scripts/client/hero_controller.js, which runs every tick.

Its card shows three numbers to tune: SPEED, JUMP and GROUND_GRIP.

5. Two small changes to the hero's script

Click Open script on the Character controller card. Two changes:

a) Coins shouldn't count as ground. The controller decides you're on the ground if you're touching anything, and that includes a coin or the flag. Change the ground check so triggers don't count:

var solid = ctx.physics.touching('hero').filter(function (id) { return !/^(coin|goal|pit)/.test(id); });
var onGround = solid.length > 0 && Math.abs(me.vy) < 40;

b) Tell the animations when you're in the air, and play a jump sound. Add the sound where the jump happens, and one line before the last line of the function:

if (onGround && ctx.input.isDown('jump')) {
    vy = -JUMP;
    ctx.client.playSound('platformer:jump');
}

ctx.state.set('air', onGround ? 'no' : 'yes');

Keep the var SPEED = … lines at the top exactly as they are. That's what the card reads.

6. The state graph

Open Advanced → State graphs… and click + New graph. Give it the ID hero_moves and choose the Sprite hero. Add six states with + State, each playing the clip with the same name. Then add the ways out of each one with + Way out of:

State→ goes towhenpriority
idle_rightjump_rightair == 'yes'3
run_leftinput:left2
run_rightinput:right1
run_rightjump_rightair == 'yes'3
run_leftinput:left2
idle_right!input:right1
jump_rightjump_leftinput:left2
idle_rightair == 'no'1

Then the same three states again for the left side, with left and right swapped. Set Starts in to idle_right and click Apply.

7. Coins

  1. Add six small yellow circles: from Shapes, an ellipse, 10 × 10. Call them coin1 to coin6.
  2. Give each one + Add component → Pickup. It becomes a trigger. Its trigger_enter event runs a script, scripts/client/coin1_pickup.js, that hides the coin and adds WORTH to score.
  3. Open each pickup script and delete the ctx.message(…) line. It's there to help while you build, and would pop up a message in the game.
  4. In each coin's Events → trigger_enter, also add the action play_sound with a coin sound. An event can run a script and actions.

Add a Label at the top: Coins: ${score} / 6.

8. The flag and the pit

  1. Draw a flag and add it as an Image. Over it, add a Box collider (from **▸ Advanced · web & desktop in the Toolbox) called goal. Give it + Add component → Trigger zone**, and on its trigger_enter add the action open_ui with the value win.
  2. Add another Box collider, pit, across the gap at the bottom. Make it a Trigger zone too, and on its trigger_enter add open_ui with the value gameover.

9. Play

Press Preview (F5). Run with the arrows, jump with Space, and watch the hero turn and switch between standing, running and jumping.

How it works

  • Components write ordinary scripts. The Character controller is 15 lines of script you can read and change. So is each Pickup. Nothing is hidden.
  • The screen's tick runs the controller. Every 16 ms, tick reads the inputs, works out the hero's speed, and hands it to the physics engine. The engine does the falling and the standing on platforms. A screen's tick runs one script, so keep one controller per screen.
  • The state graph only looks, it never moves anything. Each frame it checks the ways out of the current state, highest priority first, and switches when one is true. air comes from the controller, and input:left straight from the keyboard.
  • Triggers open screens without code. trigger_enter is an event like click, so it can have actions, including open_ui.
  • Every visit starts fresh. Try again opens level again. The coins are back and score is 0, because a screen starts as designed each time it opens.

Make it yours

  • Tune the feel: raise JUMP for floatier jumps, and lower GROUND_GRIP for slidier stops.
  • Make a second level: copy the level screen, move the platforms, and point the first level's flag at level2 instead of win.
  • Add spikes: a Trigger zone with open_ui gameover, drawn with a spiky Image.
  • Add a moving platform: an Animation that moves a platform's x back and forth, with Loop and Autoplay ticked. Give the platform a Kinematic body instead of a Static body.