我们经常在游戏里看到,人物跟随鼠标的点击旋转并移动位置,其实算法还是比较简单的。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var musX:Number;
private var musY:Number;
private var rect:Rect;
public function Main()
{
rect=new Rect();
addChild(rect);
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
this.stage.addEventListener(MouseEvent.CLICK,clickHandler);
}
private function onEnterFrame(e:Event):void{
var dx:Number=mouseX-rect.x;
var dy:Number=mouseY-rect.y;
var radians:Number=Math.atan2(dy,dx);
rect.rotation=radians*180/Math.PI+0;
}
private function clickHandler(e:MouseEvent):void{
musX=e.stageX;
musY=e.stageY;
trace(musX);
rect.x+=(musX-rect.x)/5;
rect.y+=(musY-rect.y)/5;
}
}
}