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

バネっぽい何か

バネっぽい何か
@author naoto koshikawa
Get Adobe Flash player
by naoto5959 24 Jan 2009
package
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	[SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "30")]
	/**
	 * バネっぽい何か
	 * @author naoto koshikawa
	 */
	public class PhysicalSomething2 extends MovieClip
	{
		// _____________________________________________________ Property
		/** 物体の数 */
		private const PARTICLE_COUNT:uint = 5;
		
		/** 物体の半径 */
		private const PARTICLE_SIZE:uint = 20;

		/** 重力の設定 大きくすると重力アップ。小さくすると重力ダウン */
		private const GRAVITY:Number = 980;
		
		/** 摩擦係数。摩擦により減っていくスピードの率 */
		private const FRICTIION:Number = 0.85;
		
		/** ばねの強さ */
		private const SPRING_FORCE:Number = 15;
		
		/** base container which virtual stage */
		private var _base:Sprite;
		
		/** canvas for drawing virtual spring*/
		private var _canvas:Shape;
		
		/** particle list */
		private var _particles:Array;
		
		/** previous time */
		private var _prevTime:Number;
		
		/** passed time */
		private var _t:Number;
		
		// _____________________________________________________ Method
		/**
		 * constructor
		 */
		public function PhysicalSomething2() 
		{
			_prevTime = getTimer();
			createBase();
			createParticles();
			addEventListener(Event.ENTER_FRAME, enterFrameListener);
		}

		/**
		 * create base container which move center position in virtual
		 */
		private function createBase():void
		{
			_base = new Sprite();
			_base.x = stage.stageWidth / 2;
			_base.y = stage.stageHeight / 2;
			var dotData:BitmapData = new BitmapData(2, 2, true, 0x00000000);
			dotData.setPixel32(0, 0, 0x33000000 | 0x00FFFFFF * Math.random());
			var dot:Shape = new Shape();
			dot.graphics.beginBitmapFill(dotData);
			dot.graphics.drawRect(-_base.x, -_base.y, stage.stageWidth, stage.stageHeight);
			_base.addChild(dot);
			_canvas = new Shape();
			_base.addChild(_canvas);
			addChild(_base);
		}
		
		/**
		 * create particles
		 */
		private function createParticles():void
		{
			// create particles
			_particles = [];
			var initX:Number = mouseX;
			var initY:Number = mouseY;
			for (var i:uint = 0; i < PARTICLE_COUNT; i++)
			{
				_particles.push(new Particle(PARTICLE_SIZE));
				_particles[i].alpha = 0.7;
				_particles[i].x = initX;
				_particles[i].y = initY;
				_base.addChild(_particles[i]);
			}
		}
		
		/**
		 * move to
		 * @param	particle
		 * @param	targetX
		 * @param	targetY
		 */
		private function move(particle:Particle, targetX:Number, targetY:Number):void
		{
			var dist:Number = Math.sqrt(Math.pow(targetX - particle.x, 2) + Math.pow(targetY - particle.y, 2));
			var radius:Number = Math.atan2(targetY - particle.y, targetX - particle.x);
			
			// 加速度にスプリング係数を掛ける
			particle.ax = Math.cos(radius) * dist * SPRING_FORCE;
			particle.ay = Math.sin(radius) * dist * SPRING_FORCE;
			
			// 重力加速度を加算
			particle.ay += GRAVITY;

			// 位置に加算
			particle.x += particle.vx * _t + 0.5 * particle.ax * _t * _t;
			particle.y += particle.vy * _t + 0.5 * particle.ay * _t * _t;
			
			// 速度を更新
			particle.vx += particle.ax * _t;
			particle.vy += particle.ay * _t;
			
			// 速度に摩擦係数をかける
			particle.vx *= FRICTIION;
			particle.vy *= FRICTIION;
		}
		
		
		// _____________________________________________________ Listener
		/**
		 * Event.ENTER_FRAME event
		 */
		private function enterFrameListener(event:Event):void
		{
			_t = (getTimer() - _prevTime) / 1000;
			_canvas.graphics.clear();
			_canvas.graphics.lineStyle(1, 0xcccccc);
			// マウスの位置へ移動
			_canvas.graphics.moveTo(_base.mouseX, _base.mouseY);
			// マウスの位置から最初の物体の位置へ線を引く
			_canvas.graphics.lineTo(_particles[0].x, _particles[0].y);
			move(_particles[0], _base.mouseX, _base.mouseY);
			
			for (var i:uint = 1; i < _particles.length; i++)
			{
				// 次の物体の位置へ線を引く
				_canvas.graphics.lineTo(_particles[i].x, _particles[i].y);
				move(_particles[i], _particles[i - 1].x, _particles[i - 1].y);
			}
			_prevTime = getTimer();
		}
	}
}
import flash.display.Graphics;
import flash.display.MovieClip;

class Particle extends MovieClip {
	// _____________________________________________________ Property
	public var vx:Number = 0;
	public var vy:Number = 0;
	public var ax:Number = 0;
	public var ay:Number = 0;
	// _____________________________________________________ Method
	/**
	 * constructor
	 */
	public function Particle(size:Number=20):void
	{
		var gr:Graphics = graphics;
		gr.beginFill(0xEEEEEE * Math.random(), 0.9);
		gr.drawCircle(0, 0, size);
	}
}