Java Reference
In-Depth Information
Group
and the particles are children of that
Group
. But for this example, that arrangement is not ideal.
Since all bodies in the simulation have coordinates in the same coordinate space, it makes sense to keep
all
Nodes
in a
Group
that is located at 0.0. This allows the coordinates of each node to be exactly the same
as their corresponding body.
Keeping all of the particles in a single
Group
has another advantage: It allows particles from different
Emitters
to be blended together. This is done in the code in Listing 10-1 by setting the
blendMode
of the
Group
particles to Add
. The
Nodes
that represent the
StaticBodies
are stored in the
Group
obstacles
, so
they are not included in the blend effect.
Listing 10-1 shows that a
World
object is created and animated by the
Timeline
worldUpdater
.
Bodies
are added and removed from the
world
with the functions
addWorldNode
and
removeWorldNode
. The two
functions also manage which
Groups
are used for a particular
WorldBody
.
The function
addEmitter
is used to keep track of all running
emitters
in the scene so that they can
be stopped and removed by a call to
removeEmitter
. The function
clear
resets the scene for a new
example to be displayed.
Looking back at the function
sparks
we can see that a number of
Pegs
are added. Listing 10-2 shows
the implementation of
Peg
.
Listing 10-2.
Peg.fx
public class Peg extends WorldNode, Circle{
init{
bodies[0] = new StaticBody(new net.phys2d.raw.shapes.Circle(radius));
bodies[0].setPosition(translateX, translateY);
bodies[0].setRestitution(1.0);
fill = Color.GRAY;
}
public override function update():Void{
//static bodies do not move
}
}
In Listing 10-2 we see the class
Peg
, which simply creates a circular
StaticBody
at the same location
as the
Peg
.
Peg
extends
Circle
, so the graphical representation of the
StaticBody
is taken care of.
Besides the
Pegs
, a
SparkEmitter
is involved in this example, as it is the class that creates the falling
sparks from Figure 10-1. Listing 10-3 shows the implementation.
Listing 10-3.
SparkEmitter
public class SparkEmitter extends Emitter{
public var x:Number;
public var y:Number;
var cloudTimeline:Timeline = Timeline{
repeatCount: Timeline.INDEFINITE;
keyFrames: KeyFrame{
time: 1/5.0*1s;
action: emitCloud;
}
}