Workshop › Mario-style Platformer › Step 11 of 37
A platformer hero feels better with momentum: holding a direction builds up speed bit by bit, up to MAX_SPEED,
instead of jumping to full speed at once.
```js
const ACCEL = 0.5
const MAX_SPEED = 4
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value))
}
const input = (keys.ArrowRight ? 1 : 0) - (keys.ArrowLeft ? 1 : 0)
if (input !== 0) player.vx += input * ACCEL
player.vx = clamp(player.vx, -MAX_SPEED, MAX_SPEED)
player.x += player.vx
```clamp keeps a value between min and max.input is 1 for right, -1 for left, 0 for neither or both.vx grows by ACCEL in that direction, clamped to ±4.x += vx moves the player sideways.MAX_FALL write ACCEL and MAX_SPEED.function solidAt(x, y) { write clamp.updatePlayer write the four lines and an empty line.Skills: Velocity and gravity, Keyboard and mouse input
← Which keys are held? Friction →