Game Development Reference
In-Depth Information
Our big
while
loop lasts as long as there are
Block
objects in the
blocksToCheck
array. Since we
seeded it with
blockToMatch
(the
Block
the user clicked), we drop into the loop and get started with
it. The first thing we do in the loop is
pop
the last
Block
out of the
blocksToCheck
array and put it into
tempBlock
. We then check the color value of
tempBlock
(
tempBlock.blockColor
) to see if it matches
the color of the
Block
the user clicked (
colorToMatch
).
Since we got the value for
colorToMatch
from the first
Block
, which also happens to be
tempBlock
, this comparison will be
true
. We then push
tempBlock
into the
blocksMatched
array
(because it did indeed match) and call
tempBlock.makeBlockClicked
, which will light up the
Block
by adding the glow filter we created in the
Block
class. This test is only guaranteed to evaluate
true
the first time through the
while
loop. All subsequent iterations will be actual tests on
Block
objects other than the
Block
the user clicked.
while(blocksToCheck.length > 0) {
tempBlock = blocksToCheck.pop();
if (tempBlock.blockColor == colorToMatch) {
blocksMatched.push(tempBlock);
tempBlock.makeBlockClicked();
}
Now we are going to test all the Block objects that surround
tempBlock
. Recall that we created
two arrays, rowList and colList, and their values relate to the Blocks in Figure 8-5. First, we
create a temporary Block object, tempBlock2, to use for this operation. Next, we create a for
loop that iterates through the rowList array. Since rowList and colList have the same number of
values, we don't need to specifically iterate through it, we will use the index (i) from the rowList
iteration for colList.
The next
if
statement tests to see if the
Block
represented by
rowList[i]
,
colList[i]
is within
the bounds of the
board
. The values must be greater than 0 and less than
BLOCK_ROWS
and
BLOCK_COLS
(for columns and rows respectively). If we do not do this, an “array out of bounds
exception” will be thrown at runtime.
var tempBlock2:Block;
for (var i:int = 0;i < rowList.length;i++) {
if ((tempBlock.row + rowList[i]) >= 0 && (tempBlock.row + rowList[i])
< BLOCK_ROWS && (tempBlock.col + colList[i]) >= 0 &&
(tempBlock.col + colList[i]) < BLOCK_COLS ) {
So now that we know we are looking at a legitimate place in the
board
2D array, we set
tr
equal
to the row calculation and
tc
equal to the column calculation. We now have two values that
represent one of the
Block
s in Figure 8-5. We set
tempBlock2
equal to the
Block
object in the
board
array represented by the
tr
and
tc
indexes (
board[tr][tc];
).
var tr:int = tempBlock.row + rowList[i];
var tc:int = tempBlock.col + colList[i];
tempBlock2 = board[tr][tc];
Now, we are ready for our most important test. If the blockColor property of tempBlock2 is equal
to colorToMatch (the color of the clicked Block), we are in business.
However, we are not ready to push this
Block
into the
blocksToCheck
array just yet. We also need
to check to see if this
Block
is already in either
blocksToCheck
or
blocksTested
. If we did not
check this, the same
Block
would be pushed into
blocksToCheck
multiple times, and this function
would never complete.