Java Reference
In-Depth Information
var theta = Math.toRadians(startingDirection);
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);
}
rotate += 4;
}
}
In Listing 10-8 we see the class
FireParticle
, which extends
ImageView
and
Particle
. This class
borrows many ideas from the particles in Chapter 2. We can see that the variable
initialSteps
determines how long each
FireParticle
will be present in the scene, just like before. The other
parameters work as expected Note, however, that
direction
is preset to -90. This causes the
FireParticle
to move upward like fire does. In the
init
function we can see that the variable
startingDirection
is set to the value of direction plus a random value. This gives each
FireParticle
a
little variation in its motion.
The
doStep
method also works much as expected— when
stepsRemaining
reaches zero the
FireParticle
is removed from the scene. The position of the
FireParticle
is updated by adding
deltaX
to
translateX
and adding
deltaY
to
translateY
. Lastly, the
opacity
of the
FireParticle
is set based on
how far along the
FireParticle
is in its life cycle.
Summary
This chapter presented two examples of using particle effects with a physics engine. The first example
used a body for each particle. This created a complex effect, but at the expense of performance. The
second example used one body per emitter, and each emitter created its own small particle effect. Both
techniques are excellent ways to add advanced graphics elements to any application, but especially to
games.