Game Development Reference
In-Depth Information
Finally, we add the image to the instance of
Shot
and set
finished
to
false
. This will get set to
true
once. The
Shot
has used up all its
moves
.
addChild(image);
finished = false;
}
Now it is time to write the
update()
,
render()
and
dispose()
functions for the
Shot
class.
update()
is called by the
Game
class (
FlakCannon.as
) to set the
nextLocation Point
. We do not
actually move anything in
update()
. The reason is very simple. If we had to do any kind of look-
ahead collision detection (e.g., testing for hitting walls), we would want to make sure that we have
all the
nextLocation
values for our game objects beforehand. Even though we don't use look-
ahead collision detection for this game, we will use the same conventions so you will get used to
them.
The first thing we do in
update()
is test to see if there are still
moves
left. If not, we set
finished
to
true
. The next time
Game
checks to see if the
Shot
is finished, it will turn it into a
Flak
explosion. We'll discuss that process later. If the
Shot
is not finished, we update the
nextLocation
Point
and decrement
moves
.
public function update():void {
if (moves > 0) {
nextLocation.x = x + xunits;
nextLocation.y = y + yunits;
moves--;
} else {
finished = true;
}
}
We just told you that
update()
only sets the new movement values.
render()
actually does the
work, which in this case is very simple. We set the
x
and
y
values of the
Shot
to value of
nextLocation.x
and
nextLocation.y
. Again, just like
update()
,
render()
is called by
Game
.
public function render():void {
x = nextLocation.x;
y = nextLocation.y;
}
Finally, we get to
dispose()
. When the
Shot
has finished moving, and it has been turned into a
flak explosion, we need to remove it. While
Game
will remove the
Shot
from the screen and take it
out of its
aShot[]
array,
dispose()
takes care of removing the
Bitmap
and
BitmapData
from
memory. This clean up operation, while not required, helps initiate garbage collection in AS3.
Without performing this action, eventually the game would start slowing down considerably as
available memory becomes harder and harder to locate.
public function dispose():void {
removeChild(image);
imageBitmapData.dispose();
image = null;
}
}
}