/* ************************************************ EXAMPLE: Hello World AUTHOR: Richard Leggett CREATED: September 23, 2005 MODIFIED: September 23, 2005 Flash® by Example http://www.ifbin.com ************************************************ */ /** * ParticleEmitter generates instances of the Particle class initialized * with a given speed and direction. */ import Particle; import flash.filters.*; class ParticleEmitter extends MovieClip { private var __particles:Array = null; private var __gravity:Number = 0.8; private var __maxParticles:Number = 10; private var __frequency:Number = 2; private var __speed:Number = 1; private var __angle:Number = 270; private var __blurAmount:Number = 5; private var __blurFilter:BlurFilter = null; private var __spread:Number = 8; private var __colors:Array = null; // [0xRRGGBB, ... ] public function set blur (blur:Number) : Void { __blurAmount = blur; } public function set spread (spread:Number) : Void { __spread = spread; } public function set frequency (freq:Number) { __frequency = freq; } public function set gravity (g:Number) : Void { __gravity = g; } /** * Constructor
* Initializes internal arrays and creates a blur filter. */ public function ParticleEmitter() { __particles = new Array(); __colors = new Array(); __blurFilter = new BlurFilter( __blurAmount , __blurAmount , 1 ); } /** * Initialize ParticleEmitter with a number of max particles, * a speed and a direction to "shoot" the particles in. * @param maxParticles Number of particles allowed in the emitter's memory at any one time * @param speed Scalar at which to move the particles every frame * @param angle The angle at which to fire the particles */ public function init( maxParticles:Number , speed:Number , angle:Number ) : Void { __maxParticles = maxParticles; __speed = speed; __angle = angle; } /** * Add a color used in randomly coloring the particles. * @param col The numeric color value to use */ public function addColor ( col:Number ) : Void { __colors.push( col ); } /** * Begin firing those particles! */ public function start():Void { onEnterFrame = update; } /** * Cease firing the particles. */ public function stop() : Void { delete onEnterFrame; } /** * Update the particles in the current stream, and create new ones. */ public function update():Void { // Update position of existing particles for ( var i in __particles ) __particles[ i ].updatePosition(); // Create N new particles, where N equals the frequency of the particle emitter for ( var i = 0 ; i < __frequency ; i++ ) { // Attach new particle at a unique depth var depth:Number = getNextHighestDepth(); var p:Particle = Particle( attachMovie( "Particle" , "p"+depth , depth ) ); // Move new particle to the position of the emitter p._x = _x; p._y = _y; // Set the initial velocities for the particle p.vx = __speed * Math.cos( Math.PI / 180 * __angle ) + Math.random() * __spread - __spread / 2; p.vy = __speed * Math.sin( Math.PI / 180 * __angle ); // Assign a value for gravity p.grav = __gravity; // Scale the particle to a semi-random value (betw. 10 and 79.99..) p._xscale = Math.random() * 70 + 10; p._yscale = p._xscale; // Apply the blur filter with a random offset __blurFilter.blurX = __blurFilter.blurY = Math.random() * __blurAmount + 1; p.filters = [ __blurFilter ]; // Set the particles alpha to betw. 80 and 100 p._alpha = random( 20 ) + 80; p.cacheAsBitmap = true; // Colorize the particle var col:Color = new Color( p ); col.setRGB( __colors[ random( __colors.length ) ] ); // Add the particle to the internal array __particles.push( p ); // Destroy any particles above and beyond our max particle limit if ( __particles.length >= __maxParticles ) { __particles[0].destroy(); __particles.splice( 0 , 1 ); // remove it from the array } } } }