Game Development Reference
In-Depth Information
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_PLAYS,String(plays)));
}
The
removeClickedBlocksFromScreen
function is pretty straightforward. It contains a
while
loop
that pops
Block
objects out of
clickedBlocks
and then calls
removeBlock
, passing a reference to
those
Block
objects so they can be removed from the screen. It also plays the
Main.SOUND_BONUS
sound so the player gets some audio feedback from their move.
This function also contains the scoring code for Color Drop. We create a local variable named
pointsPerBlock
and set its initial value to the difficulty setting
POINTS_PER_BLOCK
. Inside the
while
loop, we increment
pointsPerBlock
by
BONUS_PER_BLOCK
for every connected block as a bonus for
removing more blocks with one move. In our call to
addToScore
, we pass
Math.
floor(pointsPerBlock)
, which means the value rounds down until it reaches a whole number. By
rounding, the player would receive one point per
Block
up until the fifth
Block
, receive two points per
Block
for five to nine blocks, and at for ten blocks or more, receive three points per block, and so on.
This scoring prompts the player to try to configure the blocks to remove more on a single move if
possible, because the score can ramp up pretty quickly on one turn.
public function removeClickedBlocksFromScreen():void {
var blockLength:int = clickedBlocksArray.length-1;
var pointsPerBlock:Number = POINTS_PER_BLOCK;
dispatchEvent( new CustomEventSound(
CustomEventSound.PLAY_SOUND,Main.SOUND_BONUS,false,1,0,1));
while(clickedBlocksArray.length > 0) {
addToScore(Math.floor(pointsPerBlock));
tempBlock = clickedBlocksArray.pop();
removeBlock(tempBlock);
pointsPerBlock += BONUS_PER_BLOCK;
}
}
The
addToScore
function simply increments
score
by the value passed, increments
levelScore
and then reports those values with dispatched events.
public function addToScore(val:Number):void {
score += int(val);
levelScore += int(val);
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SCORE,String(score)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_LEVEL_SCORE,
String(levelScore)));
}
The
moveBlocksDown
function is the second most intricate function in this game after
findLikeColoredBlocks
. The job of this function is to locate the missing blocks that were removed
in
removeClickedBlocksFromScreen
and update how those missing
Block
objects relate to the rest
of the
Block
objects on the screen. This exercise involves finding the missing
Block
objects in
board, updating the existing
Block
objects to move into the empty positions left by the removed
Block
objects and mark the newly empty positions in the board 2D array as null so that
replaceBlocks
can replenish them.
First, we need to iterate through all the rows and columns in
board
and do it in the most efficient
way we can manage. Because the blocks move down the screen, that means that the lowest
blocks in the on-screen grid are the ones that will have a shorter distance to fall if a
Block
has