Java Reference
In-Depth Information
Listing 2-12. Particle.fx (partial)
//Other attributes ommited
public-init var gravity = 0.1;
function doStep(){
//remove particle if particle has expired
if (--stepsRemaining == 0){
delete this from (parent as Group).content;
}
//advance particle's location
translateX += deltaX;
translateY += deltaY;
deltaY += gravity;
if (fadeout){
opacity = startingOpacity*(stepsRemaining as Number)/(initialSteps as Number);
}
rotate += 4;
}
A new attribute called gravity is added. This attribute is used in the modified doStep function to
increase the value of deltaY for every step.
Further Considerations
The examples in this chapter show how to implement a basic particle system by taking advantage of
some of the graphics features of JavaFX. These examples could be used in many games or other visually
interesting applications. However, the attributes of emitters and particles can be combined in
innumerable ways to produce unique visual effects. Here are a number of other features that could be
added to a particle system to produce even more options.
Heterogeneous Particles: There is no reason why all of the particles have to be the same. Combining
particles of different shape, color, or even duration can open the door to many new possibilities. Varying
the proportion of each particle type is another way of customizing the look of an effect. For example,
consider a campfire, where the dominant particle is the fire. It is easy to imagine sprinkling in a second
particle to represent the sparks of a popping log.
Moving Emitter: Since the emitter is a node, it can be moved and animated like any other node. Moving
an emitter that produces slow-moving or even still particles will produce a snake-like effect. A moving
emitter that emits fast-moving particles can be used to create a comet-like effect.
Nonlinear Changes: In the previous examples the rate at which the particles became transparent was
linear. Using JavaFX's Interpolator class, it would be simple to implement a particle that only starts to
fade during the last 80 percent of its life. This would produce an effect with greater visual density than
the previous examples, but will maintain the soft edge provided by the fading effect.
Search WWH ::




Custom Search