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

Mouse Gesture

Get Adobe Flash player
by a24 16 Jan 2009
package  
{
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	[ SWF( width = "465" , height = "465" , backgroundColor = "0xFFFFFF" , frameRate = "60" ) ]
	
	public class MouseGesture extends Sprite
	{
		private var _startPoint : Point;
		private var _nextPoint : Point;
		private var _baseDistance : Number;
		private var _tempDirection : String;
		private var _direction : String;
		private var _sp : Sprite;
		private var _tf : TextField;
		
		/*======================================================================*//**
		 * コンストラクタ
		 */
		public function MouseGesture()
		{
			_tempDirection = "";
			_direction = "";
			_baseDistance = 20;
			
			_tf = new TextField();
			_tf.wordWrap = true;
			_tf.selectable = false;
			_tf.width = 445;
			_tf.height = 445;
			_tf.x = 10;
			_tf.y = 10;
			_tf.text = "マウスの左ボタンを押しながらカーソルを動かしてね。";
			addChild( _tf );
			
			_sp = new Sprite;
			addChild( _sp );
			stage.addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );
		}
		
		/*======================================================================*//**
		 * マウスダウン
		 */
		private function mouseDownHandler( e:MouseEvent ):void
		{
			var _color : Number = Math.random() * 0xffff00;
			_startPoint = new Point( stage.mouseX , stage.mouseY );
			_direction = "Gesture : ";
			_sp.graphics.lineStyle( 2 , _color , 1.0 );
			_sp.graphics.moveTo( _startPoint.x , _startPoint.y );
			stage.addEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
			stage.addEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
		}
		
		/*======================================================================*//**
		 * マウスムーヴ
		 */
		private function mouseMoveHandler( e:MouseEvent ):void
		{
			_nextPoint = new Point(  stage.mouseX , stage.mouseY );
			var _distance : Number = Point.distance( _startPoint , _nextPoint );
			
			// 一定の距離で線を描画、方向判定
			if ( Math.abs( _distance ) > _baseDistance )
			{
				_sp.graphics.lineTo( _nextPoint.x , _nextPoint.y );
				putDirection();
				_startPoint = new Point( stage.mouseX , stage.mouseY );
			}
		}
		
		/*======================================================================*//**
		 * マウスアップ
		 */
		private function mouseUpHander( e:MouseEvent ):void
		{
			_sp.graphics.clear();
			stage.removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
			stage.removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
		}
		
		/*======================================================================*//**
		 * 角度から方向を割りだす
		 */
		private function putDirection():void 
		{
			var _vector:Point = _nextPoint.subtract( _startPoint );
			var _angle : Number = Math.atan2( _vector.y , _vector.x )  * 180 / Math.PI;
			if ( _angle >= 45 && _angle < 125 )             _tempDirection = "↓";
			else if ( _angle >= 125 || _angle < -125 )      _tempDirection = "←";
			else if ( _angle >= -45 && _angle < 45 )       _tempDirection = "→";
			else if ( _angle >= -125 && _angle < -45 )    _tempDirection = "↑";
			if ( _direction.substr( _direction.length - 1 , 1 ) != _tempDirection ) _direction += _tempDirection;
			_tf.text = _direction;
		}
	}
}