Game Development Reference
In-Depth Information
as a parameter is the value used to decrement the
alpha
value of the
Block
when
render
is called
by
Game
.
public function startFade(fadeValue:Number):void {
this.fadeValue = fadeValue;
isFading=true;
}
This
update
function is much like the
update
functions we have already written in this topic. It
looks to see if the
Block
's
isFalling
variable equals
true
, and if so, it updates
nextYLocation
to
the current
y
position plus
speed
. The fading of the
Block
is not affected by
render
.
public function update():void {
if (isFalling) {
nextYLocation = y + speed;
}
}
The
render
function is only slightly more complicated than
update
. If the
Block
is falling,
update
sets
y
to
nextYLocation
. If
fallEndY
is reached, the function makes sure the
Block
ends in the
correct location by setting it exactly to the value in
fallEndY
. We do this because the last time the
block moved, there is a good chance that that it overshot
fallEndY
, so we simply move it to
where it should land. The
isFalling
property is set to
false
, so
ColorDrop
can test to see if all
Block
objects have stopped falling.
If the block is
fading
, we update the
alpha
value of the
Block
by subtracting the
fadeValue
. If the
alpha
value is reached (or goes negative), we set it to
0
and set
isFading
to
false
, so
ColorDrop
can test to see if all the fading
Blocks
are finished.
public function render():void {
if (isFalling) {
y = nextYLocation;
if (y >= fallEndY) {
y = fallEndY;
isFalling = false;
}
}
if (isFading) {
alpha -= fadeValue;
if (alpha <= 0) {
alpha = 0;
isFading = false;
}
}
}
The
onMouseDownListener
function is called when the player clicks on the
Block
with the mouse.
This function dispatches a custom event that we will create in the next section of this chapter. The
CustomEvent
sends a reference to itself as the second parameter.
ColorDrop
will listen for this
event, so it can start processing the
Block
objects to see which ones it should remove from the
screen.