topical media & game development

talk show tell print

animation-ch05-FollowMouse.ax

animation-ch05-FollowMouse.ax [swf] [flash] flex


  package
  {
   import flash.display.Sprite;
   import flash.events.Event;
   
   public class @ax-animation-ch05-FollowMouse extends Sprite
   {
    private var arrow:animation_ch05_Arrow;
    private var speed:Number = 5;
    
    public function @ax-animation-ch05-FollowMouse()
    {
     init();
    }
   

init(s)


    private function init():void
    {
     arrow = new animation_ch05_Arrow();
     addChild(arrow);
     addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
  

frame(s)


    private function onEnterFrame(event:Event):void
    {
     var dx:Number = mouseX - arrow.x;
     var dy:Number = mouseY - arrow.y;
     var angle:Number = Math.atan2(dy, dx);
     arrow.rotation = angle * 180 / Math.PI;
     var vx:Number = Math.cos(angle) * speed;
     var vy:Number = Math.sin(angle) * speed;
     arrow.x += vx;
     arrow.y += vy;
    }
   }
  }
  


(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.