显示对象从舞台上一个点移动到另一个点的公式
package
{
/**
* @author zoe
*
*/
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
[SWF(width =400,height = 400)]
public class Main extends Sprite
{
private var circle:Shape;
private var crtTargetX:int;
private var crtTargetY:int;
private var r:int;
private var speed:int;
private var rand:uint;
public function Main()
{
init();
}
private function init():void
{
rand = Math.random()*0xFFFFFF;
speed = 3.5;
circle = new Shape();
circle.graphics.beginFill(rand);
circle.graphics.drawCircle(40,40,30);
circle.graphics.endFill();
addChild(circle);
r =circle.width/2;
var point:Point = findRandomPoint();
crtTargetX = point.x;
crtTargetY = point.y;
trace(crtTargetX,crtTargetY);
addEventListener(Event.ENTER_FRAME,efHandler);
}
private function efHandler(event:Event):void
{
var dx:Number = crtTargetX - circle.x;
var dy:Number = crtTargetY - circle.y;
var radians:Number = Math.atan2(dy,dx);
circle.x += speed * Math.cos(radians);
circle.y += speed * Math.sin(radians);
if(dx<=speed&&dy<=speed)
{
removeEventListener(Event.ENTER_FRAME,efHandler);
}
}
private function findRandomPoint():Point
{
var p:Point = new Point();
p.x = Math.random()*(this.stage.stageWidth-2*r)+r;
p.y = Math.random()*(this.stage.stageWidth-2*r)+r;
return p;
}
}
}