topical media & game development

talk show tell print

animation-ch03-RotateToMouse.ax

animation-ch03-RotateToMouse.ax [swf] [flash] flex


  package
  {
   import flash.display.Sprite;
   import flash.events.Event;
   
   public class @ax-animation-ch03-RotateToMouse extends Sprite {
    private var arrow:Arrow;
    
    public function @ax-animation-ch03-RotateToMouse() {
     init();
    }
  

init(s)


    private function init():void {
     arrow = new Arrow();
     addChild(arrow);
     arrow.x = stage.stageWidth / 2;
     arrow.y = stage.stageHeight / 2;
     addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
  

frame(s)


    public function onEnterFrame(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;
    }
   }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.