Game Development Reference
In-Depth Information
A very easy way to check to see if a value already exists in an array is to use the Array.indexOf
function. When we pass to this function the Block object we are checking ( tempBlock2 ), it will
return -1 if it is not found in the array. This is the value we want to see. By checking for -1 in
blocksToCheck .indexOf(tempBlock2) and blocksTested.indexOf(tempBlock2) , we will know if
we should push this Block into the blocksToCheck array.
if (tempBlock2.blockColor == colorToMatch &&
blocksToCheck.indexOf(tempBlock2) == -1
&& blocksTested.indexOf(tempBlock2) == -1) {
blocksToCheck.push(tempBlock2);
}
}
}
After we have tested this Block fully, we push it into the blocksTested array, and the loop
continues to check the blocksToCheck array until it is exhausted of Block objects. When the loop
is finished, we return the entire array of blocksMatched to the caller.
You might now be thinking, “Wait, why do I need both blocksToCheck and blocksTested ? Why
can't I just use blocksTested ? Isn't it a list of the Block objects that we have already looked at?”
Well, yes that is true, but remember, we are testing up to eight blocks every time we go through
this loop. Any or all of those Block objects could be pushed into blocksToCheck , but only one,
tempBlock , will be pushed onto blocksTested . Without checking blocksToCheck , we will get into
an infinite loop nearly every time this function is executed, because multiple instances of Block
objects already in blocksToCheck , but not in blocksTested , would be pushed into blocksToCheck .
We would simply never get to the end of the blocksToCheck array.
blocksTested.push(tempBlock);
}
return blocksMatched;
}
Removing blocks
The game state GameStates.STATE_REMOVE_CLICKED_BLOCKS is set when ColorDrop has finished its
call to findLikeColoredBlocks and the GameStates.STATE_WAIT has finished its dramatic pause.
We now have an array of Block objects named clickedBlocks that we can use to calculate the
score for the player, remove Block objects from the screen, and then replenish the Block objects
in the board 2D array. The call to removeClickedBlocks from the state machine switch statement
in runGame starts this process.
removeClickedBlocks is a fairly simple function, as most of the real work is done by other functions.
First, it calls removeClickedBlocksFromScreen to remove the Block objects in the, then calls
moveBlocksDown to move the remaining Block objects in the board array to new positions based on
the missing Block objects. We then reinitialize the clickedBlocks array, to be safe, but we have
already removed the contents in the call to removeClickedBlocksFromScreen (as shown in the
following code snippet). After that, we dispatch an event to ScoreBoard to update the plays display.
When this function returns, the switch statement in runGame will change the gameState to
GameStates.STATE_START_REPLACING so the board can be replenished with Block objects.
public function removeClickedBlocks():void {
removeClickedBlocksFromScreen();
moveBlocksDown();
clickedBlocksArray = new Array();
Search WWH ::




Custom Search