补间对象的y坐标一次为0.1

时间:2014-02-23 20:58:39

标签: actionscript-3 actionscript

我有一个游戏,其中一个MovieClip的y坐标发生变化(我的重力实现),并且角色经常落在地面上(y坐标增加太多,以至于MovieClip只是跳过使角色停止下降的坐标)。为了解决这个问题,我想也许角色的y坐标应该一次增加0.1,直到角色到达while循环中的特定点。我试过这样的事情:

var yToGo = char.y + jumpPow; //jumpPow is something that increases that makes the character "fall" when not touching the ground.
while(char.y !== yToGo)
{
    char.y += char.y < yToGo ? 0.1 : -0.1;
    for(i = 0; i < fallingBlocks.length; i++) //fallingBlocks is basically the array that holds "ground"
    {
        if(charTouchingFallingBlock(i)) //checks if the character is touching any of the grounds
        {
            jumping = false; //Makes character stop falling
            char.y = fallingBlocks[i].y; //Makes character go to y of the ground it's touching
            break;
        }
    }
}

charTouchingFallingBlock函数通过将整数作为参数之一来检查角色是否触及任何地面,它看起来像:

function charTouchingFallingBlock(i)
{
    return jumpPow >= 0 && fallingBlocks[i].hitTestPoint(char.x, char.y, true) && char.y <= fallingBlocks[i].y);
}

这几乎不起作用,因为当角色“摔倒”并且最近的地面略低于角色时,SWF应用程序有时会冻结。正如你所看到的,我基本上让角色走向y坐标并在它到达时停止,或者当它接触地面时停止(阻止角色穿过地面)。

我的循环是否有问题,或者是否有一个内置函数的库可以为我做这个?

1 个答案:

答案 0 :(得分:1)

根据浮点的本质,它总是不准确的。你应该首先遍历所有的块,如果一个块阻挡了它,那就落在它上面。否则,只需将char.y更改为yToGo

固定代码:

var yToGo = char.y + jumpPow; // jumpPow is something that increases that makes the character "fall" when not touching the ground.
var fellOnBlock:Boolean = false;
for (i = 0; i < fallingBlocks.length; i++) // fallingBlocks is basically the array that holds "ground"
{
    var block:FallingBlock = fallingBlocks[i]; // I'm just assuming your classname
    if (char.x > block.x && char.x + char.width < block.x && char.y < block.y && yToGo > block.y)
    {
        jumping = false; // Makes character stop falling
        char.y = block.y; // Makes character go to y of the ground it's touching
        fellOnBlock = true;
        break;
    }
}
if (!fellOnBlock) char.y = yToGo;

char.x > block.x && char.x + char.width < block.x确保该字符位于FallingBlock之上,char.y < block.y && yToGo > block.y确保该块阻挡。如果两个检查都为真,则该字符落在块上。