Java Reference
In-Depth Information
action: emit
}
}
//animates the particles.
var animator = Timeline{
repeatCount: Timeline.INDEFINITE;
keyFrames: KeyFrame{
time: 1.0/30.0*1s
action:function(){
for (p in content){
(p as Particle).doStep();
}
}
}
}
init{
//start emitting
emitTimeline.play();
animator.play();
}
//called when a new particle should be added.
function emit():Void{
insert Particle{} into content;
}
}
In Listing 2-1, the first thing to notice about the class Emitter is that it extends the class Group , so
that particles can be added and removed from the content of the Emitter and thus be added and
removed from any Scene the Emitter is part of.
Each Emitter has an attribute called emitTimeline of type Timeline , which is used to schedule when
particles are added to the scene. The repeatCount is set to INDEFINITE and the single KeyFrame calls the
method emit after .05 seconds. The emit method adds a single Particle every time it is called. In this
way, a Particle is added to the Scene 20 times a second for the life of the Emitter .
The init method of the class Emitter is called when a new Emitter object is created. This method
simply starts the emitTimeline if it is not started already.
The class Emitter does not do much without the class Particle . The code in Listing 2-2 shows a
simple implementation.
Listing 2-2. Particle.fx
package org.lj.jfxe.chapter2.example1;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.Group;
//provide random numbers for direction of particle
var random = new java.util.Random();
public class Particle extends Circle{
Search WWH ::




Custom Search