Workshop › Candy Crush-style Match 3 › Step 16 of 29
A phase says what the game is doing: 'idle' (waiting for the player) or 'clearing'. A matching swap starts the
clearing phase with a timer of 14 frames; update, called every frame, counts it down and then collapses.
```js
let phase // 'idle' or 'clearing' (matched gems flash)
let timer
function reset() {
// ...
phase = 'idle'
}
function startClearing() {
// ...
phase = 'clearing'
timer = 14
}
function collapse() {
// ...
phase = 'idle'
}
function update() {
if (phase === 'clearing') {
timer -= 1
if (timer === 0) collapse()
return
}
}
function loop() {
update()
draw()
```reset starts idle. startClearing switches to 'clearing' and sets the timer to 14.trySwap no longer collapses at once; update does it when the timer reaches 0, and collapse switches back to
'idle'.update() before draw().let matched write let phase and let timer.reset, at the end, write phase = 'idle'.startClearing, at the end, write the two phase/timer lines.trySwap, delete the collapse() line.collapse, at the end (after the column loop), write phase = 'idle'.function cellAt(event) { write update, and call update() in loop above draw().Skills: Game states, The game loop
← Two halves of a clear Flash, and hands off →