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

Calculating Angle - bug fixed version

@see http://level0.kayac.com/2009/10/ai.php
@see http://wonderfl.net/code/9f2570d3be335d8dcd84511d2d0a77432ed1207f
Get Adobe Flash player
by 9re 30 Oct 2009
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/m3Rs
 */

package  
{
    // @see http://level0.kayac.com/2009/10/ai.php
    // @see http://wonderfl.net/code/9f2570d3be335d8dcd84511d2d0a77432ed1207f
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import net.wonderfl.utils.WonderflSWFUrl;
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import net.wonderfl.math.WMath;
	
	[SWF(width="465", height="465", frameRate="30")]
	public class Test extends Sprite
	{
		private var _currentAngle:Number = 0;
		private var _arrow:Arrow;
		private var _followingMouse:Boolean = false;
		
		private function followMouse(e:MouseEvent):void 
		{
			_followingMouse = true;
			var angle:Number = Math.atan2(mouseY - 232.5, mouseX - 232.5);
			var currentAngle:Number = _arrow.rotation * Math.PI / 180;
			var diff:Number = angle - currentAngle;
			diff %= 2 * Math.PI;
			diff += (diff < 0) ? 2 * Math.PI : 0;
			
			if (diff > Math.PI)
				diff -= 2 * Math.PI;
				
			angle = currentAngle + diff;
			angle *= 180 / Math.PI;
			
			Tweener.addTween(_arrow, {rotation: angle ,time:2.0, onComplete:removeFlag} );
		}
		
		private function removeFlag():void
		{
			_followingMouse = false;
		}
		
		public function Test() 
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var tf:TextField = new TextField;
			tf.x = 20;
			tf.width = 350;
			tf.height = 350;
			
			tf.text = "Algorithm:\n\n";
			tf.appendText("var angle:Number = Math.atan2(mouseY - 232.5, mouseX - 232.5);\n" + 
			"var currentAngle:Number = _arrow.rotation * Math.PI / 180;\n" + 
			"var diff:Number = angle - currentAngle;\n" + 
			"diff %= 2 * Math.PI;\n" + 
			"diff += (diff < 0) ? 2 * Math.PI : 0;\n\n" + 
			
			"if (diff > Math.PI)\n" + 
			"	diff -= 2 * Math.PI;\n\n" + 
				
			"angle = currentAngle + diff;\n" + 
			"angle *= 180 / Math.PI;\n\n\n\n\n\n\nTweener.addTween(_arrow, {rotation: angle ,time:2.0});");
			tf.setTextFormat(new TextFormat("_sans", 12, 0, true), 0, 10);
			
			tf.mouseEnabled = false;
			tf.selectable = false;
			addChild(tf);
			
			
			_arrow = new Arrow;
			_arrow.x = 232.5;
			_arrow.y = 232.5;
			addChild(_arrow);
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, followMouse);
		}
	}
}

import flash.display.Sprite

class Arrow extends Sprite {
	private const HUNIT:int = 8;
	private const VUNIT:int = 6;
	
	public function Arrow() {
		graphics.lineStyle(1);
		graphics.moveTo( -HUNIT, VUNIT);
		graphics.lineTo(3 * HUNIT, VUNIT);
		graphics.lineTo(3 * HUNIT, 3 * VUNIT);
		graphics.lineTo(5 * HUNIT, 0);
		graphics.lineTo(3 * HUNIT, -3 * VUNIT);
		graphics.lineTo(3 * HUNIT, -VUNIT);
		graphics.lineTo( -HUNIT, -VUNIT);
		graphics.lineTo( -HUNIT, VUNIT);
	}
}