Workshop › Candy Crush-style Match 3 › Step 15 of 29
Everything happens in one instant now: one click and the board has already changed. To show it, the work must be split
in two so the halves can happen at different moments: startClearing finds and scores the matches, collapse
removes them and lets the gems fall. matched moves up to be shared by both.
```js
let matched // the cells being cleared
startClearing()
collapse()
function startClearing() {
matched = findMatches()
score += matched.size * 10
}
// Remove the matched gems; everything above falls down to fill the gaps, and new gems drop in from the top.
function collapse() {
for (const cell of matched) board[Math.floor(cell / N)][cell % N] = -1
```matched is now declared at the top, so startClearing can fill it and collapse can read it later.startClearing finds and scores; collapse first empties the matched cells, then works as before.trySwap calls the two one after the other, so the game behaves the same for now.let score write let matched.trySwap, replace the const matched, score and for lines with startClearing().collapse comment write startClearing.collapse and make the for (const cell of matched) ... line its first line.Skills: Functions, Game states
← Let the gems fall Wait before clearing →