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

Math.atan2で角度を求める

Get Adobe Flash player
by dkgkAs 08 Apr 2009
package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	[SWF(width = 465, height = 465, backgroundColor = 0xffffff, frameRate = 30)]
	
	public class Arrow extends Sprite 
	{
		private var scx:Number;
		private var scy:Number;
		private var arrow:Sprite;
		
		public function Arrow():void 
		{
			scx = stage.stageWidth * 0.5;
			scy = stage.stageHeight * 0.5;
			
			setup();
			
			stage.addEventListener(Event.ENTER_FRAME, draw);
		}
		
		private function setup():void
		{
			arrow = makeArrow();
			arrow.x = scx;
			arrow.y = scy;
			this.addChild(arrow);
		}
		
		private function makeArrow():Sprite
		{
			var sp:Sprite = new Sprite();
			sp.graphics.lineStyle(1, 0, 1);
			sp.graphics.beginFill(0x6699cc);
			sp.graphics.moveTo( -50, -25);
			sp.graphics.lineTo(0, -25);
			sp.graphics.lineTo(0, -50);
			sp.graphics.lineTo(50, 0);
			sp.graphics.lineTo(0, 50);
			sp.graphics.lineTo(0, 25);
			sp.graphics.lineTo( -50, 25);
			sp.graphics.lineTo( -50, -25);
			sp.graphics.endFill();
			
			return sp;
		}
		
		private function draw(event:Event):void 
		{
			var dx:Number = mouseX - arrow.x;
			var dy:Number = mouseY - arrow.y;
			var radians:Number = Math.atan2(dy, dx);
			arrow.rotation = radians * 180 / Math.PI;
			
		}
		
	}
	
}