Java Reference
In-Depth Information
sparkTimeline.play();
}
public override function stop():Void{
cloudTimeline.stop();
sparkTimeline.stop();
}
}
Listing 10-3 shows the class
SparkEmitter
. This class creates two
Timelines
, one for each particle
type. The
Timeline
cloudTimeline
calls the function
emitCloud
, which creates a
PhysicsParticle
and
adds it to the
world
through the function
addWorldNode
. Likewise, the
Timeline
sparkTimeline
calls the
function
emitSpark
, which creates a
PhysicsParticle
and adds it to the
world
. The difference between
the functions
emitCloud
and
emitSpark
is just the settings used to create the particle.
The cloud particles are created using the
Image
cloud
set to a yellow color and set to live for 10 to 40
steps. Since a
PhysicsParticle
has a body, this particle will drop straight down. A linear interpolator
controls how the particle fades.
The spark particles are created based on a random size, which dictates the scale, the
startingSpeed
,
and the
totalSteps
. Large particles are slower than smaller ones, and smaller ones also live longer. The
particles start out with a random velocity in a random upward direction and use the
Image
spark
.
Listing 10-4 shows the implementation of
PhysicsParticle
.
Listing 10-4.
PhysicsParticle
public class PhysicsParticle extends Group, WorldNode, Particle{
public-init var image:Image;
public-init var radius:Number;
public-init var weight:Number;
public-init var totalSteps:Number = 100;
public-init var startingDirection:Number;
public-init var startingSpeed:Number;
public-init var fadeInterpolator:Interpolator=Interpolator.LINEAR;
var stepsLeft = totalSteps;
init{
insert ImageView{
translateX: image.width/-2.0;
translateY: image.height/-2.0;
image: image;
} into content;
var body = new Body(new Circle(radius), weight);
body.setRestitution(.8);
body.setPosition(translateX, translateY);
body.setRotation(Math.toRadians(rotate));
body.adjustAngularVelocity(5.0);
var theta = Math.toRadians(startingDirection);