Game Development Reference
In-Depth Information
For
update
, we loop through all of the
Block
objects in the
board
2D array looking for any that are
not
null
. This detail is important, because blocks can still be falling into (or out of) place moving
before the entire grid has been replenished with new
Block
objects. If we do not test for
null
values in the array, the game will throw runtime exceptions because of the null object reference. If
the
Block
is not
null
, then we test to see if
tempblock.isFalling
is
true
. If so, we call
tempBlock.update
. Otherwise, we do nothing.
public function update():void {
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 != null) {
if (tempBlock.isFalling) {
tempBlock.update();
}
}
}
}
}
The
render
function is very similar to
update
. First we check for
null
values, and then, we call
tempBlock.render
if
tempBlock.isFalling
or
tempBlock.isFading
is
true
. We did not test for
tempBlock.isFading
in
update
because
tempBlock.update
does not handle any of the alpha
fading work for the block.
public function render():void {
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 != null) {
if (tempBlock.isFalling || tempBlock.isFading) {
tempBlock.render();
}
}
}
}
}
Waiting for user input
The game state
GameStates.STATE_WAITING_FOR_INPUT
is set when
ColorDrop
is in the game loop
waiting for a mouse click. When one of the
Block
objects is clicked,
CustomEventClickBlock.EVENT_CLICK_BLOCK
is dispatched, and the function
blockClickListener
is
called to handle it (see Figure 8-4).