AS3在重力作用下顺利落地

AS3 landing smoothly under affect of Gravity

我正在尝试制作一款多平台格斗游戏。我已经成功制作了游戏的控制、移动、(双人)跳跃和重力部分。

问题是,当玩家跳跃时,到达地面后,他们似乎跳得比他们在平台上应该跳的更深(他们应该着陆并停留在平台表面)。这在玩家二段跳时更明显。

我知道为什么会这样;这是因为有时 hitTestObject 当物体进入得太快时需要一段时间才能做出反应。

所以,我首先想到的是让玩家的脚y axis等于他落地平台顶部的y axis

不过,该解决方案导致了相当不稳定的着陆。

我的问题是:有没有什么办法可以让玩家顺利降落在平台的顶面?

我尝试过的一些事情:

-提高FPS,它只是使同样的效果发生,但更快。

-降低玩家下落的速度,但这会让游戏变得不那么有趣,所以我把它划掉了。

这是我的相关代码:

stage.addEventListener(Event.ENTER_FRAME, loop);
var jumpConstant:Number = 30;
var gravityConstant:Number = 1.8;
function loop(e:Event):void //happens 
{
    if(player.leg1.foreleg1.foot1.hitTestObject(platforms.ground)||player.leg2.foreleg2.foot2.hitTestObject(platforms.ground)) //if either of the player's legs are colliding with the platform [ps: nested movieclips]
    {
        player.ySpeed = 0; //the player stops going downwards
        player.y = platforms.y; //the player's y becomes the platform's y. don't worry, it puts the player in the right place, just not smoothly.
        if (player.b_up) //if Up control button (W or Up Arrow) is being pressed
        {
            player.ySpeed = -player.jumpConstant; //make the player jump

        }
    else //if the player isn't colliding with the platform
    {
        player.ySpeed += player.gravityConstant; //the player is affected by gravity and is pulled downwards
    }

Link 想试试看生涩效果的可以进游戏:

http://www.fastswf.com/-64Ux3I

你为什么不将 hitTestObject 的 y 坐标设置在离地面稍高一点的位置,这样沉入地面实际上会使它现在接触地面。

问题是您仅以 ySpeed 的增量检查碰撞。由于 y 速度在下降过程中每步增加 1.8,因此您很快就会看到间隔很远的碰撞检查。如果你想要像素精确的碰撞检查,你需要以 1px 的增量检查碰撞。这意味着如果 y 速度为 10,则在一次循环更新期间需要进行 10 次碰撞检查。在循环函数中使用 for 循环来完成此操作。