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

study090531

練習用として
/**
 * Copyright shossho ( http://wonderfl.net/user/shossho )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/Xkj6
 */

//練習用として

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
    [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#ffffff")]
	
	public class Node extends Sprite
	{
		private var particles:Array;
		private var numParticles:uint = 30;
		private var minDist:Number = 230;
		private var springAmount:Number = .0015;
		
		public function Node()
		{
			init();
		}
		
		private function init():void
		{
			particles = new Array();
			for(var i:uint = 0; i<numParticles; i++)
			{
				var size:Number = Math.random()*7+3;
				var particle:Ball = new Ball(size, 0x000000, 0.9);
				particle.x = Math.random()*stage.stageWidth;
				particle.y = Math.random()*stage.stageHeight;
				particle.vx = Math.random()*6-3;
				particle.vy = Math.random()*6-3;
				particle.mass = size;
				addChild(particle);
				particles.push(particle);
			}
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void
		{
			graphics.clear();
			for(var i:uint = 0; i<numParticles; i++)
			{
				var particle:Ball = particles[i];
				particle.x += particle.vx;
				particle.y += particle.vy;
				if(particle.x > stage.stageWidth)
				{
					particle.x = stage.stageWidth;
					particle.vx *= -1;
				}else if(particle.x < 0){
					particle.x = 0;
					particle.vx *= -1;
				}
				if(particle.y > stage.stageHeight)
				{
					particle.y = stage.stageHeight;
					particle.vy *= -1;
				}else if(particle.y < 0){
					particle.y = 0;
					particle.vy *= -1;
				}
					
			}
			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);
				}
			}
		}
		
		private function spring(partA:Ball, partB:Ball):void
		{
			var dx:Number = partB.x - partA.x;
			var dy:Number = partB.y - partA.y;
			var dist:Number = Math.sqrt(dx*dx + dy*dy);
			if(dist < minDist)
			{
				graphics.lineStyle(1, 0x000000, 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;
			}
                }
	}
}

import flash.display.Sprite;
	
class Ball extends Sprite
{
	public var radius:Number;
	public var color:uint;
	public var vx:Number = 0;
	public var vy:Number = 0;
	public var mass:Number = 0;
	
	public function Ball(radius:Number, color:uint, alpha:Number)
	{
		this.radius = radius;
		this.color = color;
		this.alpha = alpha;
		init()
	}
	
	public function init():void
	{
		graphics.beginFill(color);
		graphics.drawCircle(0, 0, radius);
		graphics.endFill();
	}
}