Workshop › Tower Defense › Step 27 of 42
Tower defense has a rhythm: build, then defend, then build again. state is 'building' or 'wave'. While
building no enemies come; startWave sends the next wave of 6 + 2 × wave enemies.
```js
let wave
let state // 'building', 'wave' or 'over'
wave = 0
toSpawn = 0
spawnIn = 0
state = 'building'
function startWave() {
wave += 1
toSpawn = 6 + wave * 2
spawnIn = 0
state = 'wave'
}
if (state === 'wave' && toSpawn > 0) {
```startWave counts the wave up, sets how many will come, and switches to 'wave'.let toSpawn write let wave; under let spawnIn write let state.reset, write wave = 0 above toSpawn, change toSpawn = 30 to 0, and write state = 'building' under
spawnIn = 0.pointAt write startWave.update, add state === 'wave' && to the spawning if.Skills: Game states
← Health bars Space starts a wave →