A Simple Particle System

Look, it's SMOKE!!! This is a tutorial for Flash users which includes several complex concepts. I will try to explain everything as I go, but if there is some confusion, please send me an e-mail, and I will try to make it clearer. I wrote this in MX, but the concept will work for Flash 5 as well. First off, let's look at the end result.

 

 

I started this project with the goal of creating a neat .as file that would allow me to duplicate multiple objects with a single function call.
The .as file and the .fla file are included in the zip file here, but I've listed all the code below.

The "particle" is just a square that I punk and bloat, adjusting the transparency from 0 to 100 and back.

There is no code INSIDE the particle object, just the two onClipEvents on it, shown below...

I wrote my code in only 3 places.

// simple duplicate function taking parameters for SEED OBJECT & NUMBER of iterations
function dupe(name, howMany) {
for (num=1; num<=howMany; num++) {
duplicateMovieClip(name, name+"_"+num, num);
}
}

// the #include just imports my external file (above) at compile time.
#include
"dupe.as"
// this is the function call to "dupe" in the .as file
dupe("particle", 90);

"So what," you say... Now I have a dogpile of objects... How do I make my "particles" position and move independently?
...I'm glad you asked... This looks like a lot of code, but remember, the pink stuff is just comments...

onClipEvent (load) {
//set this size between 20 and 60
this.size =random(40)+20;
this._height = this.size;
this._width = this.size;
//position this particle somewhere left of the middle of the screen
this._x =random(Stage.width)+(Stage.width/2);
this._y =random(Stage.height);
//set this speed between 1 and 21
mySpeed = random(20)+1;
//start at a random point in the sequence
gotoAndPlay(random(40));
}

//

onClipEvent (enterFrame) {
// move this particle to the right
this._x -= mySpeed;
// if this particle is off screen, move to the left and start over
if ( this._x<0- this._width) {
this._x = Stage.width+this._width;
gotoAndPlay(1);
}
}

Every time the object gets duped, the OnClipEvents are duped as well, thus, using the random() function I am able to give each particle a mind of it's own!!!

I wrote this code, and, started tweaking my object, and very quickly I created something interesting. This is a very versitile and simple structure. It can be adapted easily to create and manipulate many types of particle systems. I hope you find it useful.

I changed the graphic, upped the alpha, and dropped the number of iterations in dupe from 90 to 20. Now we have an entirely different effect...


I hope this inspires... Send me an email if you use this idea somewhere. I'd love to see where you take it...

Grins,
     -J