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

forked from: particle

Get Adobe Flash player
by Kallethyselius 12 Apr 2009
// forked from sph62's particle
package  {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.filters.*;

	[SWF(backgroundColor="0x000000",width="400",height="400",frameRate="30")]

	public class Main extends Sprite {
		
		private var particles:Array;
		private var numParticles:uint = 50;
		private var minDist:Number = 100;
		private var springAmount:Number =  .0004;
		private var colors:Array =  [0xFF3300, 0xFF5555, 0xFF0000];
		
		public function Main() {
			init();
		}
		
		private function init():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

                        //filters = [new BlurFilter(2, 2, 1)];
			
			particles = new Array();
			for (var i:uint = 0; i < numParticles; i++) {
				var size:Number = Math.random() * 10 + 2;
				var particle:Ball = new Ball(size, colors, 0.15);
				particle.x = Math.random() * stage.stageWidth;
				particle.y = Math.random() * stage.stageHeight;
				particle.vx = Math.random() * 3 - 1;
				particle.vy = Math.random() * 3 - 1;
				particle.mass = size;
				addChild(particle);
				particles.push(particle);
			}
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void {
			for (var i:uint = 0; i < numParticles; i++) {
				var particle:Ball = particles[i];
				particle.x += particle.vx;
				particle.y += particle.vy;
				border(particle);
			}
			graphics.clear();

			for (i = 0; i < numParticles - 1; i++ ) {
				var partA:Ball = particles[i];
				for (var j:uint = i + 1; j < numParticles; j++ ) {
					var partB:Ball = particles[j];
					spring(partA, partB);
					//gravitate(partA, partB);
				}
			}			
		}

		private function spring(partA:Ball, partB:Ball):void {
			var dx:Number = partB.x - partA.x;
			var dy:Number = partB.y - partA.y;
			var distSQ:Number = dx * dx + dy * dy;
			var dist:Number = Math.sqrt(distSQ);
			if (dist < minDist) {
				graphics.lineStyle(1,colors[0],1-dist/(minDist));
				graphics.moveTo(partA.x, partA.y);
				graphics.lineTo(partB.x, partB.y);
				var ax:Number = dx * springAmount;
				var ay:Number = dy * springAmount;
				partA.vx += ax / partA.mass;
				partA.vy += ay / partA.mass;
				partB.vx -= ax / partB.mass;
				partB.vy -= ay / partB.mass;
			}
			
		}
		private function border(particle:Ball):void {
			if (particle.x > stage.stageWidth) {
				particle.x = 0;
			}else if (particle.x < 0) {
				particle.x = stage.stageWidth;	
			}
			if (particle.y > stage.stageHeight) {
				particle.y = 0;
			}else if (particle.y < 0) {
				particle.y = stage.stageHeight;	
			}
		}
		
		private function gravitate(partA:Ball, partB:Ball):void {
			var dx:Number = partB.x - partA.x;
			var dy:Number = partB.y - partA.y;
			var distSQ:Number = dx * dx + dy * dy;
			var dist:Number = Math.sqrt(distSQ);
			var force:Number = partA.mass * partB.mass / distSQ;
			var ax:Number = force * dx / dist;
			var ay:Number = force * dy / dist;
			partA.vx += ax / partA.mass;
			partA.vy += ay / partA.mass;
			partB.vx -= ax / partB.mass;
			partB.vy -= ay / partB.mass;
		}
		
	}
	
}

import flash.display.Sprite;

class Ball extends Sprite {
	public var _radius:Number;
	public var _colors:Array;
	public var _alpha:Number;

	public var mass:Number = 0;
	
	public var vx:Number = 0;
	public var vy:Number = 0;
	
	public function Ball(radius:Number, colors:Array, alpha:Number) {
		this._radius = radius;
		this._colors = colors;
		this._alpha = alpha;
		this.mass = radius * 5;
			
		init();
			
	}
		
	private function init():void {
		graphics.beginFill(_colors[0], _alpha);
		graphics.drawCircle(0, 0, _radius*Math.random());
		graphics.endFill();
		
                graphics.beginFill(_colors[1], _alpha);
		graphics.drawCircle(0, 0, _radius*Math.random());
		graphics.endFill();
		graphics.beginFill(_colors[2], _alpha);
		graphics.drawCircle(0, 0, _radius*Math.random());
		graphics.endFill();
		graphics.beginFill(_colors[2], _alpha);
		graphics.drawCircle(0, 0, _radius*Math.random());
		graphics.endFill();
                			
	}

}