Game Development Reference
In-Depth Information
public function update():void {
frameCounter++;
}
render()
is interesting because it controls the flow of the animation. First, it checks to see if the
frameCounter
has reached the value of
frameDelay
. If so,
setNextImage()
is called, and the
frameCounter
is zeroed out so we can start counting frames for the next image in the
images
array.
public function render():void {
if (frameCounter >= frameDelay && !finished) {
setNextImage();
frameCounter=0;
}
}
Just like in
Shot
,
dispose()
gets rid of the objects we have used to help with garbage collection.
The difference here is that we need to dispose of a whole array of images instead of just one. We
use a
for
each
loop here instead of a
for
loop.
for
each
is new to AS3, but has been around in
other languages for many years. The advantage of
for each
is that the code is a bit cleaner, and
we get a typed variable (
tempImage
) to operate with. Also of note, the following call to
tempImage.dispose()
is a call to the built-in
BitmapData
class method. We named our function the
same thing so that it would be clear as to the purpose of this code.
public function dispose():void {
removeChild(image);
for each ( var tempImage:BitmapData in images ) {
tempImage.dispose();
}
images = null;
}
}
}
Explosion
Since
Explosion
is nearly identical to
Flak
, we will only talk about the differences between the
two objects. However, there is very little to say. Besides using a different set of images and a
different amount (five instead of seven), the only real difference is that there is no
hits
variable
because we do not need to count the number of times the explosion hits anything. It is simply an
animation that plays and nothing else. Recall that the
Explosion
has five frames of animation
(see Figure 4-7). Here is the code for the
Explosion
class:
package com.efg.games.flakcannon
{
// Import necessary classes from the flash libraries
import flash.display.Shape;
import flash.display.Sprite
import flash.events.MouseEvent;