Workshop › Tower Defense › Step 14 of 42
You start with 120 gold. A click builds a tower of the selected kind on that tile and pays its cost. For now a
click builds anywhere; the next step adds the rules.
```js
let gold
let selected // the kind of tower to build
gold = 120
selected = 'arrow'
function build(col, row) {
const kind = TOWERS[selected]
gold -= kind.cost
towers.push({ col, row, kind: selected, cooldown: 0 })
}
canvas.addEventListener('pointerdown', (event) => {
const p = tileAt(event)
build(p.col, p.row)
})
```build pays the kind's cost and adds { col, row, kind, cooldown }; cooldown will count the frames until the
tower can shoot again.pointerdown fires when a mouse button or a finger goes down on the canvas.let toSpawn write let gold; under let spawnIn write let selected.reset, above toSpawn = 30 write gold = 120, and at the end selected = 'arrow'.function update() { write build and the pointerdown listener.Skills: Keyboard and mouse input, Game states
← From a click to a tile Where can you build? →