java - libgdx,运动特定

标签 java libgdx

我的移动有问题。我的目标是一旦按下按键玩家自己的宽度/高度玩家就会移动。例如:

if (Gdx.input.isKeyJustPressed(Input.Keys.A)) 
    player.position.x = player.position.x-moveSpeed;

工作精确,但我希望它更平滑,在一秒内移动精确的距离,但每一毫秒移动一点,而不是一次全部移动。

这使得玩家的移动更加流畅,但并不精确:

if (Gdx.input.isKeyJustPressed(Input.Keys.A)) 
    player.position.x = player.position.x-moveSpeed*deltaTime;

如何实现平滑且精确的过渡?

最佳答案

更改(或扩展)您的玩家类,使其包含以下内容:

class Player {
    public final Vector2 position = new Vector2(); // you already have this
    public final Vector2 target = new Vector2();
    private final float speed = 10f; // adjust this to your needs
    private final static Vector2 tmp = new Vector2(); 
    public void update(final float deltaTime) {
        tmp.set(target).sub(position);
        if (!tmp.isZero()) {
            position.add(tmp.limit(speed*deltaTime));
        }
    }
    //The remainder of your Player class
}

然后在您的渲染方法中调用:

if (Gdx.input.isKeyJustPressed(Input.Keys.A)) {
    player.target.x -= moveSpeed; //consider renaming moveSpeed to moveDistance
}
player.update(deltaTime);

关于java - libgdx,运动特定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322568/

相关文章:

java - 使用 for 循环的 addItemListener 不可能

libgdx - 如何获取子actor中某个点的世界坐标?

java - 哪个 2D 游戏引擎适用于 native 应用程序内的跨平台模块

java - 在jsp中找不到setter

java - 如何在不使用 Set 的情况下有效地从数组中删除重复项

java - 传递 fragment 的上下文

java - 如何将数据转换为二维数组并放入表中

java - 使用 Libgdx 更新相机位置

android - 如何使用Libgdx绘制网格并处理每个方 block 的点击事件

java - 渲染方法扭曲声音;更新太频繁了?