Actionscript 3 对象与对象之间';s y坐标到特定点,每次0.1

Actionscript 3 对象与对象之间';s y坐标到特定点,每次0.1,actionscript-3,actionscript,Actionscript 3,Actionscript,我有一个游戏,其中电影唇的y坐标发生变化(我的重力实现),角色经常会从地上掉下来(y坐标增加如此之多,以至于电影唇只是跳过使角色停止下降的坐标)。为了解决这个问题,我认为角色的y坐标应该一次增加0.1,直到角色到达while循环中的特定点。我试过这样的方法: var yToGo = char.y + jumpPow; //jumpPow is something that increases that makes the character "fall" when not touching th

我有一个游戏,其中电影唇的y坐标发生变化(我的重力实现),角色经常会从地上掉下来(y坐标增加如此之多,以至于电影唇只是跳过使角色停止下降的坐标)。为了解决这个问题,我认为角色的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;
        }
    }
}
这几乎不起作用,因为SWF应用程序有时会在角色“坠落”且最近的地面略低于角色时冻结。如你们所见,我基本上让角色朝着y坐标移动,当它到达时停止,或者当它接触地面时停止(阻止角色穿过地面)


我的循环是否有问题,或者是否有一个内置函数的库可以为我执行此操作?

由于浮点运算的本质,它总是不准确的。你应该先在所有的方块上打圈,如果有一个方块挡住了你的去路,就倒在上面。否则,只需将
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;
var yToGo=char.y+jumpPow;//jumpPow是指当角色不接触地面时,会使其“坠落”的东西。
var fellOnBlock:Boolean=false;
对于(i=0;iblock.x&&char.x+char.widthblock.y)
{
跳跃=false;//使角色停止下落
char.y=block.y;//使字符转到它所接触地面的y处
fellOnBlock=true;
打破
}
}
如果(!fellOnBlock)char.y=yToGo;
char.x>block.x&&char.x+char.width
确保字符位于FallingBlock上方,而
char.yblock.y
确保块挡住了方向。如果两个检查均为true,则字符将落在块上

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;