Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 equal to -1 inside the column arrays. Essentially, we update the data here so that gems above the ones removed "fall down". The actual change will take place in the animateCollapse method:

        for c = 1, constants...