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:
- 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 insidematchArray
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
- 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 theanimateCollapse
method:...