我的移动有问题。我的目标是一旦按下按键玩家自己的宽度/高度,玩家就会移动。例如:
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/