Workshop › Endless Runner › Step 9 of 19

Cacti keep coming

What we are doing

A countdown decides when the next cactus appears at the right edge. After each one, the next wait is random, but never shorter than 50 frames, so there is always room to land.

```js
let nextIn = 60 // frames until the next obstacle

function spawn() {
  obstacles.push({ x: canvas.width, y: GROUND - 40, w: 20, h: 40 })
  // At least 50 frames apart, so there is always room to land and jump again.
  nextIn = 50 + Math.floor(Math.random() * 70)
}

  nextIn -= 1
  if (nextIn <= 0) spawn()
```

What it means

Your turn

  1. Under speed, write nextIn.
  2. Under the pointer lines, write spawn.
  3. In update, write the countdown right above the line that moves the obstacles.

Skills: Game states

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