Particle Effects in Flash using an Actionscript 3 (AS3) Class Structure


(To view the previous part of this post on Particle Effects in Flash using an Actionscript 3 (AS3) Class Structure, click here!)

particle.as has already been covered in the previous post, next up is the particle_manager.as.

There’s just three functions this time, and they are all pretty easy to understand…

Constructor:

First thing is we initialize the array to hold our individual particles.  Loop though all 25 particles we wish to create, call the particle() constructor, and set a unique name for each.

var countdowntimer:Number;
public function particle_manager()
{
    countdowntimer = 0;
   
    particle_array = new Array(25);
    for(var f:Number = 0; f < 25; f++){
        particle_array[f] = new particle(275,380);
        particle_array[f].name = "P" + f;
    }
}

Splash Execution:

countdowntimer is a check to make sure all 25 particles from the previous animation are already off screen. This helps preserve the fluidity of the animation, making sure particle fountain animations don’t jump between each other abruptly, and also saves our users computer and processor from (potential) overkill.

Once we are SURE there’s no other animation already running, reset the timer, setup the position of our particles, start their individual splash functions, and add them to the screen (give them visibility).

The one other thing we need to do is add the event listener Event.REMOVED. Once any individual particle is pulled of the stage, our deletion function will be called, thereby decrementing the countdowntimer, letting the flash AS3 particle driver know how close we are to the next particle explosion iteration.

public function generate_splash(xpos:Number, ypos:Number){
    if(countdowntimer == 0){
        countdowntimer = 25;
        for(var g:Number = 0; g < 25; g++){
            particle_array[g].x = xpos;
            particle_array[g].y = ypos;
            //particle_array[g].accel = -15 + Math.random() *10;
            particle_array[g].start_splash();
            addChild(particle_array[g]);
            particle_array[g].addEventListener(Event.REMOVED, deletion);
        }
        trace(System.totalMemory / 1000000); //just a check on your RAM, see if you're using too much
    }
}

 

Cleanup:

The last function is our deletion() I already mentioned up above. All it is accomplishing is the decrement of our countdown timer, letting the flash particle driver know if we are done.

function deletion(event:Event):void {
    countdowntimer--;
}

 

Such little left to go!!  Our particle_driver.fla

The code, in its entirety, is this:

var pm:particle_manager = new particle_manager();
addChild(pm);

stage.addEventListener(MouseEvent.CLICK, clicked);

function clicked(e:MouseEvent):void{
    pm.generate_splash(mouseX,mouseY);
}

Simply construct our one particle_manager, add the MouseEvent.CLICK event, and generate the splash on execution.

 

The one last thing worth mentioning, is the actual particle art.  While it is best to check it out in the particle_driver.fla file, an explanation can’t hurt.

It is a double wrapped MovieClip, with the outside being a blue effect to hide the sharp edges of the particle splats.  On the inside MovieClip for the flash particles, I made 10 frames of random splattered images, and on loading, call a 1-line function on frame one:

gotoAndStop(Math.floor(Math.random()*10) + 1);

This helps further generate the randomness and spontaneity, making our particle effect truly unique.

 

The absolute best thing about this flash particle system (which is totally 100% free to all users, by the way), is the fact that it is wrapped in a nice manager shell.  To implement it in your flash game, website, ads, or what have you, all you need is those 5-10 lines of code instantiating the manager, setting it to a timer or listener, and firing the splashes!

Thanks for browsing yet another quality Flash Cove tutorial, and I hope you come visit again!

 

- Dave at the Flash Cove (flashcove.net)

Pages: 1 2

About Dave

Hey Guys! I'm Dave, an Ohio State CSE alumni who just recently started work in the industry. I'm a flash hobbyist at heart, and love making flash games and tutorials when the time permits. Check out what flash tutorials are available on the site, and don't forget to "Like Us!" on Facebook!