Workshop › Tower Defense › Step 20 of 42
Towers do not shoot every frame. Each has a cooldown that counts down; at zero, if it has a target, it fires a
bullet and the cooldown starts again at the kind's reload. A timer per thing, counted down in update: the same
idea runs every weapon and spell in games.
```js
let bullets
bullets = []
for (const t of towers) {
t.cooldown -= 1
if (t.cooldown > 0) continue
const target = targetFor(t)
if (!target) continue
bullets.push({ x: t.col, y: t.row, target, kind: t.kind })
t.cooldown = TOWERS[t.kind].reload
}
ctx.fillStyle = '#fef08a'
for (const b of bullets) {
ctx.beginPath()
ctx.arc((b.x + 0.5) * TILE, TOP + (b.y + 0.5) * TILE, 4, 0, Math.PI * 2)
ctx.fill()
}
```continue skips the rest of the loop for this tower and goes on to the next one.let towers write let bullets; in reset, under towers = [], write bullets = [].update, after an empty line, write the towers loop.draw, after the enemies loop and an empty line, write the bullets.Skills: Game states, The game loop