在 Slick2D 中,如何让角色在松开按键后继续移动?

How to make character continue moving after releasing key, in Slick2D?

我是 Slick2D 的新手,我正在尝试用我的角色做动作。我可以在按住移动键的同时让它平稳地移动,但我也希望角色完成它的移动以便它准确地停在下一个图块上(我有简单的地图,图块为 32x32)。这对我来说是个问题,因为它会移动到下一个瓷砖,但它会传送到那里 - 移动是即时的,我希望我的角色以相同的速度继续移动。

例如,我在 update() 方法中尝试过这样的事情:

else if (input.isKeyPressed(Input.KEY_D))
    {
        characterAnimation = characterAnimationRight;
        characterAnimation.update(delta);
        xCoord = (int) xCoord;
        while (xCoord%32 != 0)
        {
            xCoord += 1;
            characterAnimation.update(delta);
            if (xCoord > Window.WIDTH - 32)
            {
                xCoord = Window.WIDTH - 32;
            }

        }
    }

但我做不到。

你为什么不尝试使用 "xSpeed" 和 "ySpeed" 并根据 'characterAnimation' 设置的位置以及你是否在一个确切的图块上来设置这些值还是不?

类似于:

else if (input.isKeyPressed(Input.KEY_D)) {
    characterAnimation = characterAnimationRight;
}

// ...

if (characterAnimation == characterAnimationRight){
    xSpeed = 1;
}
else if (characterAnimation == characterAnimationLeft){
    xSpeed = -1;
}
xCoord += xSpeed;
characterAnimation.update(delta);
if(xCoord % 32 == 0) {
    xSpeed = 0;
}
// ...

我实际上并不知道您的代码(或 slick2d)是如何工作的,所以我假设在调用 characterAnimation.update(delta) 时神奇地考虑了 xCoord 值。否则根据 xCoord.

做任何你需要更新角色位置的事情

解决方法是不在update()方法中计算while()中的xCoord。原因是它是在 update() 方法的单个 运行 中计算的,然后调用 render() 方法来渲染字符。这意味着角色在那之后渲染 while() 并且它传送。

这是我的解决方案:

@Override
public void update(GameContainer gc, StateBasedGame s, int delta)
        throws SlickException {

    // .......

    if (moving)
    {
        if (movingDirection == DIR_RIGHT)
        {
            if (xCoord >= targetCoord)
            {
                xCoord = targetCoord;
                moving = false;
            }
            else
            {
                xCoord += delta * 0.1f;
                characterAnimation.update(delta);
                if (xCoord > Window.WIDTH - 32)
                {
                    xCoord = Window.WIDTH - 32;
                }
            }
        }
        else if (movingDirection == DIR_LEFT)
        {
            if (xCoord <= targetCoord)
            {
                xCoord = targetCoord;
                moving = false;
            }
            else
            {
                xCoord -= delta * 0.1f;
                characterAnimation.update(delta);
                if (xCoord < 0)
                {
                    xCoord = 0;
                }
            }
        }
        else if (movingDirection == DIR_UP)
        {
            if (yCoord <= targetCoord)
            {
                yCoord = targetCoord;
                moving = false;
            }
            else
            {
                yCoord -= delta * 0.1f;
                characterAnimation.update(delta);
                if (yCoord < 0)
                {
                    yCoord = 0;
                }
            }
        }
        else if (movingDirection == DIR_DOWN)
        {
            if (yCoord >= targetCoord)
            {
                yCoord = targetCoord;
                moving = false;
            }
            else
            {
                yCoord += delta * 0.1f;
                characterAnimation.update(delta);
                if (yCoord > Window.WIDTH - 32)
                {
                    yCoord = Window.WIDTH - 32;
                }
            }
        }
    }
}

变量moving在按下移动键后设置为真。现在,在每次调用 render() 之后,角色都会在 update() 中移动一点点,并在这个新位置呈现,直到它恰好在图块上。