Particles

I guess I should define what I mean by a particle. For me, and for the purposes of this chapter, a particle is simply a single unit, generally in the company of several (or many) other similar units. Thus, a particle could be a speck of dust, a beach ball, or a planet.

Particles generally share a common type of behavior, but also can have their own individuality . Followers of object-oriented programming will see an analogy here to objects . All objects of a particular class usually share the same behavior, defined by the methods of the class, and individual instances are customized by assigning values to various properties. Youve already seen how this works with the ball movie clips youve been using in the examples. Each had its own properties: velocity, mass, size , and so on, but all of the balls moved with the same rules.

Actually, creating a class for a particle is a great way to proceed. In New Masters of Flash Volume 3 (another friends of ED book, published in 2004), I walked through the creation of just such a class. For this book, however, Ill keep things on the timeline.

Why dont you go ahead and create some kind of graphic to use as a particle. For these examples, Ill stick with the red ball Ive been using all along (Im really proud of that thing!), but make it a lot smallerjust 10 pixels across. Whatever you come up with, convert it to a movie clip, keep the registration point centered as usual, and call it particle . Make sure that it is also set to export for ActionScript, with the linkage name particle .

The general setup will be the same in each case. Most of the variations will be in the interaction and attraction between the particles, and they will come in the onEnterFrame function. The setup basically consists of attaching a bunch of particles and sprinkling them randomly around the screen. Heres that code:

 var numParticles:Number = 30; init(); function init():Void {       for(var i:Number=0;i<numParticles;i++)       {             var particle:MovieClip = attachMovie("particle",                                                  "p" + i, i);             particle._x = Math.random() * Stage.width;             particle._y = Math.random() * Stage.height;             particle.vx = 0;             particle.vy = 0;             particle.mass = 1;       } } 

Here, you initialize each particles vx and vy to 0, and leave them at their default size. Youre also continuing to use mass, setting the particle mass to 1. Youll probably want to try changing that around to get different effects later. You might also want to start off the particles with a random velocity or randomly size them.

All the rest of the examples in this chapter assume this basic setup, and will occasionally add something to it. But mainly Ill be describing the onEnterFrame and any other functions that need to be written. First, Ill describe some background theory, but hold on to the file you just created, because youll be getting back to it very soon.



Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net