Game Development Reference
In-Depth Information
checkForFallingBlocks
is called when we need to find out if any Block objects on the screen are
currently moving. The function loops through all the Blocks in the board 2D array, and if any one
Block is still moving (
isFalling = true
), it returns false.
public function checkForFallingBlocks():Boolean {
var isFalling:Boolean = false;
for (var r:int = 0; r < board.length; r++) {
for (var c:int = 0; c < board[r].length; c++) {
tempBlock =board[r][c];
if (tempBlock.isFalling) {
isFalling = true;
}
}
}
return isFalling;
}
checkForFadingBlocks
is called when we need to find out if any
Block
objects on the screen are
currently fading. The function loops through all the Blocks in the
board
2D array, and if any one
Block
is still fading (
isFading = true
), it returns
false
.
public function checkForFadingBlocks():Boolean {
var isFading:Boolean = false;
for (var r:int = 0; r < board.length; r++) {
for (var c:int = 0; c < board[r].length; c++) {
tempBlock =board[r][c];
if (tempBlock.isFading) {
isFading = true;
}
}
}
return isFading;
}
Adding blocks to the screen
When the
gameState
is equal to
GameStates.STATE_START_REPLACING
,
runGame
calls
replaceBlocks
to put our
Block
objects on the screen. We will write this function in a way that it will be universal for
our game: no matter when blocks need to be replaced (at the beginning of a level or after the user
clicks and removes blocks), it will operate the same way. We replace blocks very simply: we loop
through our 2D array stored in
board
, and when we find a
row
,
col
combination that is equal to null
(
board[r][c] == null
), we call
addBlock(r,c)
to fill it. That's it.
addBlock
then does most of the
interesting work.
public function replaceBlocks():void {
for (var r:int = 0; r < board.length; r++) {
for (var c:int = 0; c < board[r].length; c++) {
if (board[r][c] == null) {
board[r][c] = addBlock(r,c);
}
}
}
}