Workshop › Endless Runner › Step 5 of 19

Jump

What we are doing

A jump is just an upward speed: Space or the up arrow sets vy to -11, and gravity does the rest, slowing the rise and bringing the runner back down.

```js
const JUMP = -11 // speed at the start of a jump (negative = up)

function jump() {
  if (onGround()) runner.vy = JUMP
}

document.addEventListener('keydown', (event) => {
  if ((event.key === ' ' || event.key === 'ArrowUp') && !event.repeat) jump()
})
```

What it means

Your turn

  1. Under GRAVITY, write JUMP.
  2. Under onGround, write jump and the keydown listener.

Skills: Keyboard and mouse input, Velocity and gravity

All steps

  1. The ground
  2. The runner
  3. Draw again and again
  4. Gravity
  5. Jump
  6. Short hops
  7. Tap to jump
  8. Things that come at you
  9. Cacti keep coming
  10. Wait for the start
  11. Crash
  12. Try again
  13. Faster and faster
  14. Remember the best run
  15. The score counter
  16. Birds
  17. Which keys are down?
  18. Duck
  19. Build it yourself: jump buffering