Workshop › Candy Crush-style Match 3 › Step 18 of 29

Cascades

What we are doing

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'
  }
}
```

What it means

Your turn

  1. Above let phase write let chain, and add 'falling' to the comment of let phase.
  2. In trySwap, write chain = 0 above startClearing().
  3. In startClearing, count the chain and multiply the points.
  4. At the end of collapse, change 'idle' to 'falling'.
  5. In update, under the 'clearing' block, write the 'falling' block.

Skills: Game states

All steps

  1. A place for the board
  2. A checkered grid
  3. The game loop
  4. A board of random gems
  5. Draw the gems
  6. Start without matches
  7. Click a gem
  8. Show the selection
  9. Swap two neighbours
  10. Find every match
  11. No match, no swap
  12. A score and a fresh start
  13. Clear the match
  14. Let the gems fall
  15. Two halves of a clear
  16. Wait before clearing
  17. Flash, and hands off
  18. Cascades
  19. Where a gem came from
  20. Slide down
  21. Out from under the top
  22. Is there any move?
  23. Never stuck
  24. A hint after five seconds
  25. Show the hint
  26. Twenty moves
  27. The end of a game
  28. Remember the best score
  29. Build it yourself: swipe to swap