topical media & game development
animation-ch05-FollowMouse2.ax
animation-ch05-FollowMouse2.ax
[swf]
[flash]
flex
package
{
import flash.display.Sprite;
import flash.events.Event;
public class @ax-animation-ch05-FollowMouse2 extends Sprite
{
private var arrow:animation_ch05_Arrow;
private var vx:Number = 0;
private var vy:Number = 0;
private var force:Number = 0.5;
public function @ax-animation-ch05-FollowMouse2()
{
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 ax:Number = Math.cos(angle) * force;
var ay:Number = Math.sin(angle) * force;
vx += ax;
vy += ay;
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.