Java Reference
In-Depth Information
var initialSteps = 100;//number of steps until removed
var deltaX;//change in x location per step
var deltaY;//change in y location per step
init{
radius = 5;
fill = Color.RED;
//Set radnom direction, squere technique.
deltaX = 1.0 - random.nextFloat()*2.0;
deltaY = 1.0 - random.nextFloat()*2.0;
}
package function doStep(){
//remove particle if particle has expired
if (--initialSteps == 0){
delete this from (parent as Group).content;
}
//advance particle's location
translateX += deltaX;
translateY += deltaY;
}
}
In this example, Particle extends Circle so its visual appearance is that of a red dot with a radius of
5. Particle has three attributes, duration , deltaX, and deltaY . The attribute duration tracks how long a
Particle has been in the scene. The attributes deltaX and deltaY describe how the Particle moves. We
will look at these last two attributes again after we examine how particles are animated.
As stated above, each Particle is responsible for determining how it travels. The implementation of
how a Particle travels is captured in the method doStep , which updates the location of the Particle for
a single step of its animation.
In order to animate the Particle , the doStep function will be called 30 times a second. For performance
reasons, a single static Timeline called animator is used to animate all particles in the scene. The static
sequence named particles keeps track of which particles are still in the scene and should be animated.
When a Particle is created, it sets deltaX and deltaY to a random value in the range of -1.0 to 1.0.
The Particle also inserts itself into the sequence particles so that the Timeline animator will call the
doStep function. Lastly, animator is started if it is not already running.
The doStep function first decreases the value of duration by one and checks to see if the new value is
equal to zero. If so, the Particle has reached the end of its life cycle and should be removed. To remove
a Particle, it must be removed from its parent and also from the sequence particles . Removing the
Particle from its parent removes the particle from the scene, while removing the Particle from
particles stops doStep from being called.
Lastly, the doStep method updates the location of the Particle by incrementing translateX and
translateY by deltaX and deltaY respectively. The attributes deltaX and deltaY were set to a random
value, causing each Particle to travel linearly away from the Emitter in a random direction. The method
for generating the random values of deltaX and deltaY in Listing 2-2 has a few limitations. One limitation is
that Particles traveling diagonally appear to be moving faster than particles moving vertically and
horizontally. Another limitation is that the particle system will take on a square shape as the visual
density increases. I'll discuss other methods for generating these delta values in a later example.
Search WWH ::




Custom Search