Workshop › Candy Crush-style Match 3 › Step 18 of 29
Fallen and new gems can line up by themselves: a cascade. After a collapse the game is 'falling'; then it looks for
new matches and clears again, each round of the chain worth more: 10, 20, 30 points a gem.
```js
let chain // how many clears in a row this move has caused
let phase // 'idle', 'clearing' (matched gems flash) or 'falling'
function trySwap(a, b) {
// ...
chain = 0
startClearing()
return true
}
function startClearing() {
matched = findMatches()
chain += 1
score += matched.size * 10 * chain // cascades are worth more and more
// ...
}
function collapse() {
// ...
phase = 'falling'
}
function update() {
// ...
if (phase === 'falling') {
// Landed: new matches make a cascade; otherwise the move is over.
if (findMatches().size > 0) startClearing()
else phase = 'idle'
}
}
```collapse ends in 'falling'. On the next frame update checks the board: new matches start another clear,
otherwise the move is over.let phase write let chain, and add 'falling' to the comment of let phase.trySwap, write chain = 0 above startClearing().startClearing, count the chain and multiply the points.collapse, change 'idle' to 'falling'.update, under the 'clearing' block, write the 'falling' block.Skills: Game states
← Flash, and hands off Where a gem came from →