Game Development Reference
In-Depth Information
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.GRID_SIZE_X do
column = self.grid[c]
newColumn = {}
i = 1
while #newColumn < #column do
if (#column > i) then
if (column[i] ~= -1) then
--move gem
table.insert(newColumn, column[i])
end
else
--create new gem
Search WWH ::




Custom Search