forked from: forked from: Wonderfl Book vol.03 Loop&MouseLocus

by genny forked from forked from: Wonderfl Book vol.02 Loop (diff: 67)
forked from: forked from: Wonderfl Book vol.03 Loop&MouseLocus

♥0 | Line 117 | Modified 2010-02-16 20:47:01 | MIT License
play

ActionScript3 source code

/**
 * Copyright genny ( http://wonderfl.net/user/genny )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/Alot
 */

package
{
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.filters.GlowFilter;
	[SWF(backgroundColor="0x000000", frameRate="30")]
	public class LikeTimeLine3 extends Sprite
	{
		//	Property
		private var _ball:Sprite;
		private var _timeGage:Sprite;
		private var _locus:Sprite;
		
		private var _startX:int = 0;
		private var _startY:int = stage.stageHeight/2;
		private var _endX:int = stage.stageWidth;
		private var _frameCount:int = 0;
		private var _frameCountLimit:int = 150;
		private var _animationFrames:Array;
		
		
		//	Constructer
		function LikeTimeLine3()
		{
			init();
		}
		
		
		//	initialize
		private function init() :void
		{
			
			//	フレーム数分の座標値を算出
			_animationFrames = generateAnimationFrames();
			
			
			//	ボールを生成
			_ball = new Sprite();
			_ball.graphics.beginFill( 0x66CC33 );
			_ball.graphics.drawCircle( 0, 0, 5 );
			_ball.graphics.endFill();
			addChild( _ball );
			_ball.x = _startX;
			_ball.y = _startY;
			_ball.filters = [ new GlowFilter( 0x66CC33, 1, 20, 20, 2, 2) ];
			
			
			//	タイムゲージの生成
			_timeGage = new Sprite();
			_timeGage.graphics.beginFill( 0x333333 );
			_timeGage.graphics.drawRect( 0, 0, stage.stageWidth, 5 );
			_timeGage.graphics.endFill();
			addChild( _timeGage );
			_timeGage.x = 0;
			_timeGage.y = stage.stageHeight - 5;
			_timeGage.width = 0;
			
			
			//	円の軌跡を保持する
			_locus = new Sprite();
			addChild(_locus);
			
			//	軌跡を初期化する
			updateLocus();
			
			//	レンダリング開始
			start();
			
			//	マウスイベントを登録
			stage.addEventListener( MouseEvent.MOUSE_DOWN, onRecStart );
			stage.addEventListener( MouseEvent.MOUSE_UP, onRecEnd );
		}
		
		/**
		*	軌跡描画を行う
		*/
		private function updateLocus() :void
		{
			_locus.graphics.clear();
			_locus.graphics.lineStyle( 1, 0xFFFFFF, 0.2 );
			var len:int = _frameCountLimit;
			for( var i:int = 0; i < len; i ++ )
			{
				_locus.graphics.drawCircle( _animationFrames[ i ].x, _animationFrames[ i ].y, 5 );
			}
		}
		
		
		/**
		*	Handler
		*/
		private function onRecMouse( event:Event ) :void
		{
			if( _frameCount <= _frameCountLimit )
			{
				//	Arrayの値を上書き
				_animationFrames[ _frameCount ].x = mouseX;
				_animationFrames[ _frameCount ].y = mouseY;
				updateLocus();
			}
		}
		
		private function render( event:Event ) :void
		{
			if( _frameCount <= _frameCountLimit )
			{
				_ball.x = _animationFrames[ _frameCount ].x;
				_ball.y = _animationFrames[ _frameCount ].y;
			}
			else
			{
				_ball.x = _startX;
				_ball.y = _startY;
				_frameCount = 0;
			}
			
			//	タイムゲージの伸縮
			_timeGage.width = ( _frameCount / _frameCountLimit ) * stage.stageWidth;
			_frameCount ++;
			
			trace(_frameCount);
		}
		
		
		/**
		*	Listener
		*/
		private function onRecStart( event:MouseEvent ) :void
		{
			addEventListener( Event.ENTER_FRAME, onRecMouse );
		}
		
		private function onRecEnd( event:MouseEvent ) :void
		{
			removeEventListener( Event.ENTER_FRAME, onRecMouse );
		}
		
		// ------------------------------------------------------------ //
		
		private function start() :void
		{
			addEventListener( Event.ENTER_FRAME, render );
		}
		
		private function stop() :void
		{
			removeEventListener( Event.ENTER_FRAME, render );
		}
		
		
		/**
		*	移動座標の算出を行う
		*/
		private function generateAnimationFrames() :Array
		{
			var arr:Array = new Array();
			var tmpX:Number = _startX;
			var tmpY:Number = _startY;
			var len:int = _frameCountLimit;
			var easeRatio:Number = 0.2;
			
			for( var i:int = 0; i <= len; i ++ )
			{
				tmpX += ( _endX - tmpX ) * easeRatio;
				arr.push( new Point( tmpX, tmpY ) )
			}
			return arr;
		}
		
		
	}
}