Workshop › Endless Runner › Step 9 of 19
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()
```spawn adds a 20×40 cactus standing on the ground, just past the right edge.update, the countdown goes down by one each frame and spawns at zero.speed, write nextIn.spawn.update, write the countdown right above the line that moves the obstacles.Skills: Game states
← Things that come at you Wait for the start →