Actionscript 3 使用AS3停止闪光移动对象

Actionscript 3 使用AS3停止闪光移动对象,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我用AS3编程一个对象,以一定的速度从屏幕上的某个点移动到另一个点。我尝试了不同的代码,但我不能真正实现我所寻找的。。。 现在,我正在使用以下代码: var xVelocity:Number = 8; addEventListener (Event.ENTER_FRAME, onVelocity); function onVelocity (eventObject:Event):void { animal0.x += xVelocity; animal0.y += yVe

我用AS3编程一个对象,以一定的速度从屏幕上的某个点移动到另一个点。我尝试了不同的代码,但我不能真正实现我所寻找的。。。 现在,我正在使用以下代码:

var xVelocity:Number = 8;

addEventListener (Event.ENTER_FRAME, onVelocity);

function onVelocity (eventObject:Event):void
{
    animal0.x +=  xVelocity;
    animal0.y +=  yVelocity;
}
物体完美地移动,但我不能让它停在我想要的位置x…它一直移动,直到它到达屏幕的尽头。。。 我怎样才能让它停在我想要的地方?或者如果你有更好的方法


谢谢

假设动物在屏幕上从左到右移动,一旦到达xDest,下面的代码将阻止它移动

var xVelocity:Number = 8;
var xDest = 300;//Destination point along X axis

addEventListener (Event.ENTER_FRAME, onVelocity);

function onVelocity (eventObject:Event):void
{
    if(animal0.x < xDest)
    {
        //only move animal0 so long as it has not reached the destination
        animal0.x +=  xVelocity;
        animal0.y +=  yVelocity;
    }
}

你可以试试tweening引擎,我认为greensock套装是你最好的选择。

使用距离公式计算对象距离其目的地的距离,如果距离足够近,则将其锁定到精确坐标

var dist:Number; // The distance between the object and its destination
var threshold:int = 3; //How close it has to be to snap into place
function onVelocity (eventObject:Event):void
{
    animal0.x +=  xVelocity;
    animal0.y +=  yVelocity;
    dist = Math.sqrt(Math.pow(xDest - animal0.x,2) + Math.pow(yDest - animal0.y,2));
    if(dist < threshold)
    {
        removeEventListener(Event.ENTER_FRAME, onVelocity);
        animal0.x=xDest; // Locks the object into the exact coordinates
        animal0.y=yDest;
    }

}

我在制作一款游戏时遇到了完全相同的问题,我就是这样解决的。

哇……真是太好了。开始看起来很复杂,但你解释得很好…谢谢。那真是太好了@不客气。请将答案标记为已接受,并放弃对有用答案的投票,以便其他人知道答案已解决。