0

我使用 AS3 来编程一个对象以一定的速度从屏幕中的某个点移动到另一个点。我尝试了不同的代码,但我无法真正实现我想要的......现在我正在使用以下代码:

var xVelocity:Number = 8;

addEventListener (Event.ENTER_FRAME, onVelocity);

function onVelocity (eventObject:Event):void
{
    animal0.x +=  xVelocity;
    animal0.y +=  yVelocity;
}

物体移动完美,但我不能让它停在我想要的位置 x...它继续移动直到它到达屏幕的末端...我怎样才能让它停在我想要的点?或者如果你有更好的方法来做到这一点......

谢谢

4

3 回答 3

0

假设动物在屏幕上从左到右移动,下面的代码将在它到达 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;
    }
}
于 2012-06-27T18:56:29.330 回答
0

您可以尝试补间引擎,我认为 greensock 包是您的最佳选择。 http://www.greensock.com/tweenmax/

于 2012-06-27T19:48:10.640 回答
0

使用距离公式计算对象与目的地的距离,然后如果足够近,将其锁定到您的确切坐标。

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;
    }

}

我正在创建的游戏遇到了完全相同的问题,这就是我解决它的方法。

于 2012-06-27T19:48:15.893 回答