Java Reference
In-Depth Information
return result;
}
public function randomFromNegToPos(max:Number):Number{
if (max == 0.0){
return 0.0;
}
var result = max - random.nextFloat()*max*2;
return result;
}
Listing 2-10 shows there are two new attributes to the class
Emitter
:
particleHue
and
particleHueVariation
. The attribute
particleHue
can be used to adjust the color of the
Particles
and
the attribute
particleHueVariation
determines how much to randomly adjust the color of each
Particle
.
The
emit
function of class
Emitter
is slightly changed. Now, the attributes
scaleX
and
scaleY
are set
to control the size of the particle. The attribute
effect
is set on the
Particle
to a
ColorAdjust
effect. This
effect alters the color of the node it is attached to; in this case, we adjusting the
hue
of the
Particle
. This
is what changed the color of the
Particles
in Example 5 from red to blue.
The function
normalizeHue
simply makes sure that the value passed to
hue
is of the correct range,
-1.0 - 1.0. When dealing with random numbers it is easy to accidentally set a bad value.
The function
randomFromNegToPos
is a utility function for generating random numbers from a
negative max to a positive max.
Example 6: Direction
When trying to reproduce some effects, it becomes clear that having the particles free to travel in any
direction is undesirable. For example, when simulating a candle's fire, the particles should move
upward. However, a candle's fire also flickers a little; adding a slight variation to each particle's direction
will help simulate that effect. The following example explores how to set the general direction of each
particle, as well as dictate the range of variation. The screenshot in Figure 2-9 shows a fire effect.