Datasheet

Chapter 1: Understanding Flash3D
17
Creating a Parametric Particle System (Torus Worm)
In the previous section, you learned how to get a single element orbiting a torus using the parametric
equations of a torus. Here, you multiply that single element by 100 and create a parametric particle
system, which orbits (or worms around) your torus.
You get a more formal treatment of particles in the “ Gaming ” section of this book. But for the most
part, anything with more than one element can be treated as a particle system. Treating elements as
particles has a number of advantages. Primarily, particles are easily handled mathematically.
You ’ ll use particle systems extensively throughout this book.
Building a particle system in Flash is relatively easy, and in this section, you cover the basics. To build
the orbiting worm around a torus (parametric particle system), follow the steps below:
1. Declare the number of particles to be created and create an array to place your particles in. This
is the key to working with particles. Using the array “ particles_ary ” you ’ re able to control
position, color, and alpha of each particle.
var numOfParticles:uint = 100;
var particles_ary:Array = [];
2. Use a “ for ” loop to run the updateStage() function, which creates your particles. Once the
particles are created run the addEventListener method, which starts updating the loop for each
particle on every frame event.
for(var i:uint = 0; i < numOfParticles; i++)
{
updateStage();
numVar++;
//Start Looping
if(numVar==numOfParticles){
addEventListener(Event.ENTER_FRAME, myonEnterFrame);}
}
3. Draw your balls (particles) to the stage and place them in a particle array. A random color is
assigned to each ball using the Math.random()*0xffffff method.
//Draw a Ball
var ball:Sprite = new Sprite
//Assign a random ball color
ball.graphics.beginFill(Math.random()*0xffffff);
//draws your ball at 0,0
ball.graphics.drawCircle(0, 0, ballRadius);
ball.graphics.endFill();//ends the fill
//Add ball to the stage
addChild(ball);
//Push the ball into a particle array
particles_ary.push(ball);
4. With each onEnterFrame loop you update all your particle positions using myAngle+i/20
that separates the different particles in angle by the ratio of i/20. This causes your particles to
line up on the torus and move across its surface as the myAngle variable is iterated. Use the
c01.indd 17c01.indd 17 12/14/09 3:03:28 PM12/14/09 3:03:28 PM










