Game Development Reference
In-Depth Information
blocksToCheck : This is the list of Block s that we determine are the same color as the
clicked Block clicked and are also adjacent to that block or a block of that color that is
adjacent to that block.
blocksMatched : This array contains the list of Block s that we have already determined
match the clicked Block .
blocksTested : This is list of Block s we have already tested.
Why do we need all three arrays? They are used to make sure that our iterative function only
makes the number of tests required and does not go into an infinite loop checking Block objects
that have already been checked or matching Block objects that have already been matched.
public function findLikeColoredBlocks(blockToMatch:Block):Array {
var blocksToCheck:Array = new Array();
var blocksMatched:Array = new Array();
var blocksTested:Array = new Array();
The next two arrays are also extremely important. They represent the row and column locations
in the board 2D array that we are going to test for every Block . Since we allow any Block that is
adjacent to the clicked Block (or a Block adjacent to that Block , etc.) to be considered connected,
we need to test the nine spots around any Block to see if they contain Block s with matching
colors. Figure 8-5 illustrates the row and column locations that we need to test and how they
relate to the values in rowList and colList .
var rowList:Array = [-1, 0, 1,-1,1,-1,0,1];
var colList:Array = [-1,-1,-1, 0,0, 1,1,1];
Figure 8-5. Row and column locations of surrounding blocks to check
In our first operation, we store the blockColor of the Block that was clicked in colorToMatch . We
will use this very soon.
var colorToMatch:Number = blockToMatch.blockColor;
Next, we push blockToMatch (the block passed as a parameter to this function) into the
blocksToCheck array. We need to seed this array to get started with our iteration. The rest of the
calculations will be taken care of completely by the while loop that follows.
blocksToCheck.push(blockToMatch);
Now, we start the big loop. This is a pretty complicated loop, so why not stop for a bit? Take a
breath, maybe even get a beverage then come back to this one. Done? Good. OK, let's get started.
Search WWH ::




Custom Search