O'Reilly logo

Cocos2d-x by Example : Beginner's Guide - Second Edition by Roger Engelbert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – collapsing the grid and repeating

So the flow of the game is move pieces around, look for matches, remove those, collapse the grid and add new gems, look for matches again, and if necessary, do the whole process in a loop:

  1. This is the longest method in the game, and again, most of the logic happens inside callbacks. First we tag the gems being removed by setting their type data to -1. All the gems inside matchArray will be removed:
    function GameScene:collapseGrid ()
        for i = 1, #self.gridController.matchArray do
            self.grid[self.gridController.matchArray[i].x]
            [self.gridController.matchArray[i].y] = -1
        end
    
        local column = nil
        local newColumn = nil
        local i
  2. Next, we traverse the grid's columns and rearrange the gems whose type is not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required