Game Development Reference
In-Depth Information
package com.efg.games.colordrop
{
import flash.filters.GlowFilter;
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.efg.framework.TileSheet;
import com.efg.framework.BlitSprite;
public class Block extends BlitSprite {
Now, we will create the instance variables for
Block.blockColor
is the color of the
Block
. Its value
is one of the
Block.BLOCK_COLOR_XXX
static class variables defined in the next listing. Next, we
have a set of two Boolean values that we will use to describe what the block is currently doing.
These are the mini states of the
Block
, but they are so simple that we are not going to use a state
machine. These two mini states are
isFalling
(the
Block
is moving down the screen) and
isFading
(the
Block
is fading out). For the actual fading of the
Block
, we have a variable named
fadeValue
that is set to the amount to fade the block on every frame. For falling,
fallEndY
is the
y
value on the screen that a
Block
must reach before
isFalling
is set to
false
. Finally, we have
speed
, which is how fast a block moves when falling; and
nextYLocation
, which is set in
update
and used in
render
like previous games in this topic.
public var blockColor:Number;
public var isFalling:Boolean;
public var isFading:Boolean;
public var fadeValue:Number = .05;
public var fallEndY:Number;
public var speed:int = 10;
public var nextYLocation:int = 0;
Two other very useful instance variables are
row
and
col
. These two variables represent the
current row and column in the grid kept in
ColorDrop
. They are critical for determining which
blocks need to be tested to see if they are same color as the one clicked.
public var row:Number;
public var col:Number;
The class variables in Block are all static vars that represent the colors in the tile sheet (see
Figure 8-2). These numbers will be used when calculating what part of the tile sheet to cut out to
make the Block.
public static const BLOCK_COLOR_RED:int = 0;
public static const BLOCK_COLOR_GREEN:int = 1;
public static const BLOCK_COLOR_BLUE:int = 2;
public static const BLOCK_COLOR_VIOLET:int = 3;
public static const BLOCK_COLOR_ORANGE:int = 4;
public static const BLOCK_COLOR_YELLOW:int = 5;
public static const BLOCK_COLOR_PURPLE:int = 6;
The constructor for
Block
requires information both for its superclass and for itself.
blockColor
,
row
,
col
, and
speed
are all pretty much self-explanatory from the previous descriptions.
endY
will be passed to
startFalling
, which will, in turn, set
fallEndY
to its value. However,
tileSheet
, an instance of the
TileSheet
object, needs some further discussion, as it is
essential when we call
super
.