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

5000 Boxes

5000 boxes
drawPath is pretty fast
Get Adobe Flash player
by shapevent 06 Aug 2009
/**
 * Copyright shapevent ( http://wonderfl.net/user/shapevent )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/gwmG
 */

package {
	
        // 5000 boxes
        // drawPath is pretty fast
	import flash.display.*;
	import flash.events.*;
	
	
	public class Boxes extends MovieClip {
		private const TWO_PI:Number =  Math.PI * 2;
		private var boxNum:int;
		private var pointNum:int;
		private var rot:Vector.<Number>;
		private var posX:Vector.<Number>;
		private var posY:Vector.<Number>;
		private var velX:Vector.<Number>;
		private var velY:Vector.<Number>;
		private var scale:Vector.<Number>;
		private var rotSpeed:Vector.<Number>;
		private var geometry:Vector.<Number>;
		private var cmds:Vector.<int>;
		private var stageWidth:Number;
		private var stageHeight:Number;

		
		public function Boxes(){
		   // init
			boxNum = 5000;
			pointNum = boxNum * 10;
			rot = new Vector.<Number>();
			posX = new Vector.<Number>();
			posY = new Vector.<Number>();
			velX = new Vector.<Number>();
			velY = new Vector.<Number>();
			scale = new Vector.<Number>();
			rotSpeed = new Vector.<Number>();
			geometry = new Vector.<Number>();
			cmds = new Vector.<int>();
			stageWidth = stage.stageWidth;
			stageHeight = stage.stageHeight;
			populateGeom();
			
			
			addEventListener(Event.ENTER_FRAME, onLoop);
			

		}
		// private methods

		private function populateGeom():void{
			for (var i:int = 0; i<pointNum; i+=10){
				posX.push(Math.random() * stageWidth);
				posY.push(Math.random() * stageHeight);
				velX.push(Math.random()*4 - 2);
				velY.push(Math.random()*4 - 2);
                                if (int(Math.random()*100) == 1){
                                 scale.push(Math.random()* 1 + .2);
                                }else{
				scale.push(Math.random()* 0.2 + .2);
                                }
				rot.push(Math.random() * TWO_PI);
                                
				rotSpeed.push(Math.random() * 0.5 - 0.1);
				geometry[i] = 0;
				geometry[i + 1] = 0;
				cmds.push(1);
				geometry[i + 2] =  0;
				geometry[i + 3] =  0;
				cmds.push(2);
				geometry[i + 4] =  0;
				geometry[i + 5] =  0;
				cmds.push(2);
				geometry[i + 6] = 0;
				geometry[i + 7] =  0;
				cmds.push(2);
				geometry[i + 8] =  0;
				geometry[i + 9] =  0;
				cmds.push(2);
			}
		}
		private function run():void{
			var inc:int = 0;
			var xt:Number, yt:Number, s:Number, r:Number, rs:Number;
			var cos:Number, sin:Number;
			var cosN:Number, cosP:Number, sinN:Number, sinP:Number;
			for (var i:int = 0; i<pointNum; i+=10){
				xt = posX[inc] += velX[inc];
				yt = posY[inc] += velY[inc];
				if (xt <0 || xt> stageWidth){
					velX[inc] *= -1;
				}
				if (yt <0 || yt> stageHeight){
					velY[inc] *= -1;
				}
				s = scale[inc];
				r = rot[inc] += rotSpeed[inc];
				inc++;
				cos = Math.cos(r);
				sin = Math.sin(r);
				cosN = -10  * cos;
				cosP = 10  * cos
				sinN = -10 * sin;
				sinP = 10 * sin;
				geometry[i] = xt + (cosN - sinN) * s;
				geometry[i + 1] = yt + (cosN + sinN) * s;
				geometry[i + 2] = xt + (cosP - sinN) * s;
				geometry[i + 3] = yt + (cosN + sinP) * s;
				geometry[i + 4] = xt + (cosP - sinP) * s;
				geometry[i + 5] = yt + (cosP + sinP) * s;
				geometry[i + 6] = xt + (cosN - sinP) * s;
				geometry[i + 7] = yt + (cosP + sinN) * s;
				geometry[i + 8]  = geometry[i];
				geometry[i + 9]  = geometry[i + 1];
			}
		}
		private function onLoop(evt:Event):void {
			 run();
			 graphics.clear();
			 graphics.beginFill(0x000000);
			 graphics.drawPath(cmds, geometry,GraphicsPathWinding.NON_ZERO);
			 graphics.endFill();
		}
		

	}

}