Java Reference
In-Depth Information
Listing 2-7. Particle.jx (Partial)
public class Particle extends Circle{
public-init var initialSteps:Integer;//number of steps until removed
public-init var startingOpacity = 1.0;
public-init var speed:Number;//pixels per step
public-init var fadeout = true;
var deltaX;//change in x location per step
var deltaY;//change in y location per step
var stepsRemaining = initialSteps;
init{
//radius = 5;
fill = Color.RED;
opacity = startingOpacity;
//radom direction in radians
var theta = random.nextFloat()*2.0*Math.PI;
deltaX = Math.cos(theta)*speed;
deltaY = Math.sin(theta)*speed;
}
package 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;
if (fadeout){
opacity = startingOpacity*(stepsRemaining as Number)/(initialSteps as Number);
}
}
}
The attributes of the class Particle have changed to accommodate the transparency features. A new
attribute called startingOpacity sets the opacity of the particle as it is created. The Boolean attribute
fadeout controls whether the particle should fade as it animates. The doStep function is reconfigured a
little to support the transparency. Instead of decrementing duration on each call, a new attribute called
stepsRemaining is decremented. The ratio of stepsRemaining to duration describes how far along the
Particle is in its life cycle.
If fadeout is set to true, the opacity of the Particle is set to a fraction of its startingOpacity based
on how old it is. This provides the fadeout effect for each particle.
Search WWH ::




Custom Search