| Build phase | Example value or method | Planning use |
|---|---|---|
| Game loop | requestAnimationFrame(update) called inside update() | Schedule the next frame from inside the update function, so logic and drawing run together from the start. |
| Canvas | 600×500 canvas, cleared each frame with ctx.fillRect(0,0,width,height) | A fixed canvas size makes the camera and level width easy to reason about later. |
| Level data | Array of single-character tile codes such as x, g, a, s | Store the level as data so collision and drawing read the same grid. |
| Collision | On floor contact, setBottom(tileTop – 0.01) and vely set to 0 | Snap the player to the tile edge and cancel vertical speed whenever a downward move hits a solid tile. |
| Jump physics | gravity 0.3 and friction 0.9 per frame; Phaser: gravity y 300, jump velocity -330 | Treat these as starting points to tune, not targets. Engines apply the numbers differently. |
The values come from a freeCodeCamp forum side-scroller example and a Phaser platform game tutorial.
To learn how to make a platformer in JavaScript, build a game loop, a tile map, separate-axis tile collision and a jump driven by gravity, in that order. Treat every number in the examples as a tunable starting value.
If the game loop is new to you, how to make pong in javascript is a smaller project to try first. For the wider picture of shipping a game in the browser, see how to make a browser game. The phases below build one platformer in sequence, and each phase leaves the game in a playable state.
Set up the canvas and game loop
A platformer needs a canvas to draw on and a loop that updates and redraws it every frame. The freeCodeCamp example is plain JavaScript on an HTML5 Canvas, with a 600×500 canvas and a getContext(“2d”) call to get the drawing context.
- Create the canvas in your page and fetch its drawing context once, outside the loop.
- Write one update function that moves the player, resolves collisions and then draws the frame.
- Schedule the next frame by calling requestAnimationFrame(update) inside update(). That is how the freeCodeCamp example keeps itself running, and it asks the browser to call the function again for the next frame.
- Clear the canvas at the start of drawing. The example paints a background colour with ctx.fillRect(0,0,width,height) over the whole canvas.
- Read keys as state. Store which keys are down and check that state inside update. Phaser follows the same idea: its update() is called continuously by the game loop, and continuous movement polls the key state there.
For drawing, two canvas calls cover almost everything. Use fillRect for solid tiles while you prototype. Switch to drawImage when you have art. The example’s drawImage call takes a source rectangle and a destination rectangle (image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight), so one sprite sheet can supply many frames. Its player frames are 50×84. Start with coloured boxes and swap in sprites last, so art problems never block mechanics.
Build the tile map
A tile map stores the level as a grid of codes, one per cell, instead of as a list of rectangles. The freeCodeCamp example keeps the level as an array of single-character strings such as x, g, a and s. Each character stands for a tile type, and the same array drives both drawing and collision.

MakeCode’s platformer tutorial shows the same thinking in an editor: the tilemap’s width and height are set by the last two numbers, usually 16 by 16 tiles, and you must place wall tiles for collision to happen. Collision is a property you assign to tile types, not something every tile has.
Decide up front which codes are solid. Empty air, decoration and solid ground can then be told apart by a single lookup. To find the cell under any point, divide its coordinates by the tile size and round down. Collision, drawing and the camera all rely on that conversion, so write it once as a helper.
Move the player and resolve tile collision
Tile-based collision treats the player as an axis-aligned bounding box, a rectangle with x, y, width and height, and tests it only against the tiles it overlaps. The freeCodeCamp example gives its box edge helpers, such as getBottom returning y + height, so collision code can compare edges to tile edges directly.
Move and resolve one axis at a time
Separate-axis resolution is what keeps walls and floors from snagging the player. Moving diagonally and then guessing which edge was hit is the usual cause of sticking and corner glitches. Do the horizontal move completely before the vertical one.
- Apply horizontal velocity to x, after multiplying by friction if you want the player to slow down when keys are released.
- Check the overlapped tiles. If any is solid, push the box to the tile’s near edge and set horizontal velocity to zero.
- Apply vertical velocity to y after adding gravity.
- Check the overlapped tiles again. Moving down, place the bottom just above the tile top and zero the vertical velocity. The example does this with setBottom(tileTop – 0.01) and vely = 0. Moving up, place the top below the tile and zero it the same way.
- Mark the player grounded only when a downward collision was resolved this frame.
Phaser’s Arcade Physics wraps the same idea. A collider treats the objects as solid and resolves their contact, and it exposes ground contact through player.body.touching.down.
Stop the player falling through floors at high speed
Falling through floors happens when a single frame’s movement is large enough to carry the box past a thin tile without ever overlapping it. Three habits prevent it. Resolve the vertical axis against every tile the box passes through, not just the one it ends in. Always snap to the tile edge and zero the vertical velocity on contact, as the example does. Cap the fall speed so one frame never moves the player further than a tile is tall. If you add dashes or other fast moves, apply the same cap or step the movement in smaller increments.
Add jump physics with tunable values
A jump is an upward launch speed plus a constant pull back down. On a canvas, y grows downward, so the launch is a negative vertical velocity and gravity is a positive amount added to vertical velocity every frame. The freeCodeCamp example uses a gravity of 0.3 and a friction of 0.9 per frame. Phaser’s tutorial sets gravity y to 300 and a jump velocity of -330 when the player is on the ground, with horizontal speed of plus or minus 160.

Those figures use different scales, so compare their proportions rather than copying them between engines. MakeCode’s tutorial gives ranges you can use as a tuning guide.
| Setting | Example value | Planning use |
|---|---|---|
| MakeCode low-gravity Y acceleration | 150 to 200 | Start here for a game with long, hanging jumps. |
| MakeCode high-gravity Y acceleration | around 300 to 500 | Start here for a heavier game with quick landings. |
| MakeCode jump velocity | negative Y velocity between -150 and -400 | Pick the strength to match the gravity you chose. |
| Phaser example | gravity y 300, jump velocity -330, horizontal speed 160 | A complete, working trio of values to copy as a baseline. |
Ranges are from the MakeCode platformer basics tutorial, and the single-engine example is from the Phaser tutorial. All are example values, not requirements.
To make a jump feel right, change one value at a time and replay the same jump. Gravity and jump velocity work as a pair: a stronger launch with the same gravity goes higher, while stronger gravity with the same launch makes the arc shorter. Only allow the jump when the player is grounded. In plain JavaScript use the flag from your collision step. In Phaser, check body.touching.down.

Scroll the camera along the level
A side-scrolling camera is a single horizontal offset that you subtract from every draw position. Keep all physics and collision in world coordinates and apply the offset only when drawing. That way, scrolling never changes how a collision behaves.
- Follow the player. Set the camera x so the player sits near the horizontal middle of the canvas, and recompute it every frame after movement is resolved.
- Offset everything. Subtract the camera x from tiles, the player and any enemies. Mixing offset and non-offset drawing makes objects appear to slide.
- Draw only what is visible. Loop over the tile columns that fall inside the canvas rather than the whole level, which keeps wide levels cheap.
Engines package this as a single setting. In MakeCode you select camera follow on your player sprite when the game starts. The plain JavaScript version is only a few lines, and it is a good exercise because it makes the world-versus-screen split explicit.
Troubleshooting common problems
- Movement pauses, then stutters when holding a key. Operating-system key repeat can pause and then stutter, which is why continuous movement should poll key state each frame instead of acting on repeated keydown events.
- The player jumps in mid-air. Clear the grounded flag at the start of each frame’s vertical move and set it only when a downward collision resolves. A flag that is never cleared allows endless jumps.
- Blank space shows at the level edges. Clamp the camera between the level’s left edge and the level width minus the canvas width.
- Nothing collides. Check that the tile codes you treat as solid match the codes in the level array. In MakeCode, walls must exist for collision to happen.
Next steps after the first level
Once the player runs, jumps and scrolls, extend the tile codes with hazards, pickups and a goal, since the level is only data. When you are ready to think about what a finished game could earn, html5 game monetization covers the options for browser titles.
Frequently asked questions
Should I use Phaser or plain JavaScript for a platformer?
Plain JavaScript teaches the loop, collision and camera directly, which is the point of this guide. Phaser’s Arcade Physics handles gravity and collisions for you and is a reasonable next step once you understand what it does.
What is the difference between a collider and an overlap in Phaser?
A collider treats objects as solid and resolves their contact, so the player stands on platforms. An overlap only reports that two objects touch, which suits pickups and triggers.
How long does a full platformer tutorial take?
It depends on the scope. One Kaboom.js platformer tutorial runs 7 hours. Keep a first project to the core mechanics above, and add features only once those feel right.



