Java Reference
In-Depth Information
}
public override function stop():Void{
emitTimeline.stop();
}
}
The interesting thing about the class
Fireball
as shown in Listing 10-7 is that
Fireball
extends
Group
,
WorldNode
,
Particle
, and
Emitter
. It extends
Group
so it can contain the
FireParticles
it
produces. It extends
WorldNode
and
Particle
because it is has to bounce around the screen, and these
interface classes provide that functionality. Lastly,
Fireball
extends
Emitter
, and it does this so its
Timeline
—
emitTimeline
—will be started and stopped.
For the most part,
Fireball
is just like the other
Emitters
in this topic: It creates a particle that
animates in some way.
Fireball
is also much like the other nodes used with the physics engine. It
creates a
Body
that it uses in the method
update
to synchronize its location on the screen with its location
in the physics model.
Fireball
adds some other features to the
update
method. For example, if the
Fireball
is off the
screen to the left, it is moved to the right side. And if it is off to the right, it will be moved to the left so it
will fly back onto the screen. This is why the fireballs wrap around the screen. If the
Fireball
is below a
certain level, it removes itself.
The
update
function also adjusts the location of the
FireParticles
the
Emitter
has created. This is
done to keep the location of the particles steady while the
Emitter
moves about the screen.
The particles created by
Fireball
are defined by the class
FireParticle
. Listing 10-8 shows the
implementation of
FireParticle
.
Listing 10-8.
FireParticle.fx
public class FireParticle extends ImageView, Particle{
public-init var initialSteps:Integer;//number of steps until removed
public-init var startingOpacity = 1.0;
public-init var speed:Number;//pixels per step
public-init var fadeout = true;
public-init var direction = -90.0;
public-init var directionVariation = 10.0;
var deltaX;//change in x location per step
var deltaY;//change in y location per step
var stepsRemaining = initialSteps;
init{
smooth = true;
translateX -= image.width/2.0;
translateY -= image.height/2.0;
rotate = Math.toDegrees(Main.random.nextFloat()*2.0*Math.PI);
opacity = startingOpacity;
//random direction in radians
var startingDirection = direction + Main.randomFromNegToPos(directionVariation);