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

Spring Box

画面内のどこでも Click, Click, Click!
Get Adobe Flash player
by daniwell 04 Apr 2012
/**
 * Copyright daniwell ( http://wonderfl.net/user/daniwell )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wt05
 */

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[SWF(backgroundColor = "0xffffff", frameRate = "30", width = "465", height = "465")]
	public class SpringBox extends Sprite
	{
		private var _xsprings :/*SpringManager*/Array = [];
		private var _ysprings :/*SpringManager*/Array = [];
		
		public function SpringBox() 
		{
			for (var i :int = 0; i < 4; i ++)
			{
				_xsprings[i] = new SpringManager(0, stage.stageWidth / 2);
				_ysprings[i] = new SpringManager(0, 0);
			}
			
			stage.addEventListener(MouseEvent.CLICK, _click);
			stage.addEventListener(Event.ENTER_FRAME, _enterFrame);
		}
		
		/* CLICK */
		private function _click ( evt :MouseEvent ) :void 
		{
			var hw :int = _rand(20, 80), hh :int = _rand(10, 60);
			
			for (var i :int = 0; i < 4; i ++)
			{
				// X
				_xsprings[i].setEndValue(mouseX - hw + (int(i == 1 || i == 2) * 2 * hw));
				_xsprings[i].setParam(_rand(0.1, 0.4), _rand(0.7, 0.9));
				
				// Y
				_ysprings[i].setEndValue(mouseY - hh + (int(i / 2) * 2 * hh));
				_ysprings[i].setParam(_rand(0.1, 0.4), _rand(0.7, 0.9));
			}
		}
		/* ENTER FRAME */
		private function _enterFrame ( evt :Event ) :void 
		{
			graphics.clear(); graphics.lineStyle(0, 0); graphics.beginFill(0xeeeeee);
			
			for (var i :int = 0; i < 5; i ++)
			{
				var px :Number = (i < 4) ? _xsprings[i].update() : _xsprings[0].nowValue;
				var py :Number = (i < 4) ? _ysprings[i].update() : _ysprings[0].nowValue;
				if (i == 0) graphics.moveTo(px, py); else graphics.lineTo(px, py);
			}
		}
		
		/* RANDOM NUMBER (MIN-MAX) */
		private function _rand ( min :Number, max :Number ) :Number { return (Math.random() * (max - min) + min); }
	}
}

class SpringManager {
	
	private var _nowValue :Number;
	private var _endValue :Number;
	
	private var _v        :Number = 0;
	private var _spring   :Number = 0.2;
	private var _friction :Number = 0.8;
	
	public function SpringManager ( startValue :Number, endValue :Number )
	{
		_nowValue = startValue;
		_endValue = endValue;
	}
	
	public function setParam ( spring :Number, friction :Number ) :void { _spring = spring; _friction = friction; }
	
	public function setEndValue ( value :Number ) :void { _endValue = value; }
	
	public function update ( ) :Number
	{
		_v += (_endValue - _nowValue) * _spring;
		_v *= _friction;
		_nowValue += _v;
		
		return _nowValue;
	}
	
	public function get nowValue ( ) :Number { return _nowValue; }
	public function get endValue ( ) :Number { return _endValue; }
}