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

アニメーションの勉強1

矢印を常にマウスがある位置へ向けさせる。
Get Adobe Flash player
by matsu4512 23 Feb 2009
    Embed
//矢印を常にマウスがある位置へ向けさせる。

package
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class RotateToMouse extends Sprite
	{
		private var arrow:Sprite = new Sprite();
		
		public function RotateToMouse()
		{
			makeArrow();

			addChild(arrow);  //画面に表示
			arrow.x = stage.width;
			arrow.y = stage.height;
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void{
			var dx:Number = mouseX - arrow.x;  //マウスと矢印間の距離を求める
			var dy:Number = mouseY - arrow.y;
			var radius:Number = Math.atan2(dy, dx);  //角度の計算
			arrow.rotation = radius * 180 / Math.PI;  //度からラジアンへ直す
		}
	
		private function makeArrow()  //矢印を生成
		{
			arrow.graphics.lineStyle(1,0,1);
			arrow.graphics.beginFill(0xffff00);
			arrow.graphics.moveTo(-50, -25);
			arrow.graphics.lineTo(0, -25);
			arrow.graphics.lineTo(0, -50);
			arrow.graphics.lineTo(50, 0);
			arrow.graphics.lineTo(0, 50);
			arrow.graphics.lineTo(0, 25);
			arrow.graphics.lineTo(-50, 25);
			arrow.graphics.lineTo(-50, -25);
			arrow.graphics.endFill();
		}
	}
}