In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Particules firework

Get Adobe Flash player
by Yukulele 18 Apr 2009
package  {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Transform;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import flash.text.TextField;
	
	public class Particules extends Sprite{
		
		private var number:int;
		private var max:int;
		
		private var parts:Array;
		private var forces:Array;
		
		private var myBlur:BlurFilter;
		private var bmp:Bitmap;
		private var bmpd:BitmapData;
		private var m:Matrix;
		
		private var i:int;
		private var gravite:Number;
		private var friction:Number;
		private var couleur:int;
		private var txt:TextField ;
		private var lestage:Object;
		private var leSprite:Sprite;
		private var laShape:Shape ;
		
		private var updateTimer:Timer ;
		private var genTimer:Timer ;
		
		public function Particules()
		{
                        var r:Sprite=this;
			// recupere le stage
			lestage = this.stage;
			
			//cree un sprite
			leSprite = new Sprite();
			laShape = new Shape();
			
			r.addChild( leSprite );
			
			//nombre de particules generees au clic
			number= 10;
			max= 1000; //limite
			
			//forces
			gravite = 0.1;
			friction = 0.5;
			
			//tableaux de particules et de forces
			parts = new Array();
			forces = new Array();
			
			//bitmap
			myBlur = new BlurFilter( 1.5, 3, 1);
			m = new Matrix();
			var fac:Number = .3;
			m.scale(fac, fac);
			bmpd = new BitmapData(Math.ceil( lestage.stageWidth * fac) , Math.ceil( lestage.stageWidth * fac), true, 0xFF000000 );
			
			bmp = new Bitmap(bmpd, "auto", true);
			bmp.height = bmp.width /= fac;
			
			leSprite.addChild( bmp );
			leSprite.addChild(laShape);
			
			
			//cree un champ de sortie
			txt = new TextField();
				txt.x = txt.y  = 0;
				txt.width = lestage.stageWidth;
				txt.height = 16 ;
				txt.selectable = false;
				txt.text = 'nombre : ' + number;
				txt.textColor = 0xFFFFFF;
			r.addChild( txt );
			
			
			// click
			lestage.addEventListener( MouseEvent.MOUSE_DOWN,	startGen 	);
			lestage.addEventListener( MouseEvent.MOUSE_UP, 		stopGen		);
			
			//intervalle de generation des particules
			genTimer = new Timer(20, 0);
			genTimer.addEventListener (TimerEvent.TIMER, eventGen );
						
			// une boucle d'upadte
			updateTimer = new Timer(10, 0);
			updateTimer.addEventListener (TimerEvent.TIMER, update);
                        updateTimer.start();
		}
		
		public function startGen( e:MouseEvent ):void
		{
			couleur = 
			Math.floor(Math.random() * 0xff)
			+ Math.floor(Math.random() * 0xff) * 0x100
			+ Math.floor(Math.random() * 0xff) * 0x10000;
			genTimer.start();
			
		}
		public function stopGen( e:MouseEvent ):void
		{
			
			genTimer.stop();
			
		}
		

		
		public function eventGen( t:TimerEvent ):void
		{
			
			if (parts.length < max )generate( number );
			
		}
		public function generate( i:int ):void
		{
			
			while ( i-->0  ) {
				
				parts.push
				( 
					{
					x: leSprite.stage.mouseX ,
					y: leSprite.stage.mouseY,
					couleur:couleur
					} 
				);
				forces.push
				( 
					{
					x: (Math.random()-.5)*3, 
					y: -(3+Math.random() * 3)
					}  
				);
			
			}
			
		}
		
		public function update( e:TimerEvent ):void
		{
			laShape.graphics.clear();			
			var tmpP:Array = new Array();
			var tmpF:Array = new Array();
			
			var i:int = parts.length;
			
			while ( i-->0  ) {
				
				parts[ i ].x += forces[i].x;
				parts[ i ].y += ( forces[i].y += gravite );
				
				if ( parts[ i ].x < 0 					)		forces[ i ].x = - forces[ i ].x * friction;
				if ( parts[ i ].x > lestage.stageWidth 	)		forces[ i ].x = - forces[ i ].x * friction;
				if ( parts[ i ].y < 0 					)		forces[ i ].y = - forces[ i ].y * friction;
				
				//d├ętruit les morts
				if ( parts[ i ].y < lestage.stageHeight )
				{
					tmpP.push( parts[ i ] );
					tmpF.push( forces[ i ] );
					laShape.graphics.lineStyle(1,parts[ i ].couleur );
					laShape.graphics.moveTo( parts[ i ].x  , parts[ i ].y );
					laShape.graphics.lineTo( parts[ i ].x+1, parts[ i ].y );
				}
			}
			bmpd.draw( laShape, m  );
			bmpd.applyFilter(bmpd, bmpd.rect, new Point( 0, 0 ),  myBlur );
			
			parts = tmpP;
			forces = tmpF;
			
			//compteur de particules
			txt.text = 'nombre : '+parts.length;
			
		}
		
	}
}