Game Development Reference
In-Depth Information
Ensure that we generate a different random series of gems each time we run the
game by changing the randomseed value.
The enabled property will stop user interactions while the grid is being altered
or animated.
The grid is a two-dimensional array of columns of gems. The magic happens in
the getVerticalUnique and getVerticalHorizontalUnique meth-
ods.
2. To ensure that none of the gems will form a three-gem-match on the first two
columns, we check them vertically:
function GameScene:getVerticalUnique (col, row)
local type = math.floor (math.random () *
#constants.TYPES + 1 )
if (self.grid[col][row-1] == constants.TYPES[type]
and self.grid[col][row-2] ~= nil and
self.grid[col][row-2] == constants.TYPES[type]) then
type = type + 1;
if (type == #constants.TYPES + 1) then type =
1 end
end
return type
end
All this code is doing is checking a column to see if any gem is forming a string
of three connected gems of the same type.
3. Then, we check both vertically and horizontally, starting with column 3:
function GameScene:getVerticalHorizontalUnique (col,
row)
local type = self:getVerticalUnique (col, row)
if (self.grid[col - 1][row] ==
constants.TYPES[type] and self.grid[col - 2][row] ~=
nil and self.grid[col - 2][row] ==
constants.TYPES[type]) then
local unique = false
while unique == false do
type = self:getVerticalUnique (col, row)
Search WWH ::




Custom Search