【问题标题】:Tween object's y coordinate to a specific point 0.1 at a time一次将对象的 y 坐标补间到特定点 0.1
【发布时间】:2014-02-23 20:58:38
【问题描述】:

我有一个游戏,其中 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 坐标移动并在到达时停止,或者在接触地面时停止(阻止角色穿过地面)。

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

【问题讨论】:

    标签: actionscript-3 actionscript


    【解决方案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 &gt; block.x &amp;&amp; char.x + char.width &lt; block.x 确保角色位于 FallingBlock 上方,char.y &lt; block.y &amp;&amp; yToGo &gt; block.y 确保该方块挡在路上。如果两个检查都为真,则角色落到方块上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-17
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2021-11-26
      • 1970-01-01
      • 2010-09-13
      相关资源
      最近更新 更多