Game Development Reference
In-Depth Information
The
TileSheet
(
ts
) was created and passed to
Block
by
ColorDrop
.
Block
does not own its own
TileSheet
, which means we don't have to create multiple versions of the
TileSheet
for each
Block
and waste memory. Alternatively, we could have made
tileSheet
a static class variable.
However, we think that passing in the value for the
tileSheet
helps keep the
Block
class a bit
more universal and reusable. As well, it keeps us from having to write any code to determine if
tileSheet
exists yet and if it needs to be initialized as a class variable.
The next thing we need to do is determine the tile from the sheet that will represent this
Block
's
color. Since the color is stored in
blockColor
, and the values of the
Block.BLOCK_COLOR_XXX
static
constants represent the order of the colors on the tile sheet, we can easily calculate which tile will
represent this
Block
like so:
tile = blockColor;
Finally, we call the constructor of
BlitSprite
with the code
super(tileSheet, [tile], 0)
. The
first parameter is the
TileSheet
, which we have already described. The second parameter is an
array of tile indexes for this instance to animate through. Since our
Blocks
do not animate, we
simple put a single value in the array (the value of
tile
). The third parameter is the index of the
starting tile in the tiles array. Since we only have one tile, the index is
0
.
We also need to have this
Block
fire off a
MouseEvent
if the player clicks it, so we add a
MOUSE_DOWN
event that will call the function
onMouseDownListener
. We also set
buttonMode
and
useHandCursor
to
true
, so when the mouse pointer rolls over the
Block
, we will prompt to the user
to click with a recognizable hand cursor. Finally, we call
startFalling
to have the
Block
start
moving down the screen.
public function Block(color:Number,tileSheet:TileSheet,row:Number,col:Number,
endY:Number,speed:Number) {
blockColor = color;
this.row = row;
this.col = col;
isFalling = false;
isFading = false;
var tile:Number = blockColor;
super(tileSheet, [tile], 0);
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownListener, false, 0, true);
this.buttonMode = true;
this.useHandCursor = true;
startFalling(endY,speed);
}
The
startFalling
function is the first link in developing this game into a drop-style game where
blocks fall into place. The heart of the function is the speed and
fallEndY
instance variables. The
distance between the starting y position of the Block (set by Game) and
fallEndY
should be
evenly divisible by speed, but it does not have to be. If it is, the Blocks will fall into place
smoother, but the gameplay won't be affected. We set the
isFalling
variable to true so that
update and render functions will process the
Block
movement when they are called by
Game
.
public function startFalling(endY:Number,speed:Number):void {
this.speed = speed;
fallEndY = endY;
isFalling = true;
}
The
startFade
function is called by
ColorDrop
to start the
Block
fading from the screen when the
level ends. The fade is stopped by setting the
isFading
Boolean to
true
. The
fadeValue
passed