我对运动有疑问。我的目标是,一旦按下以获得自己的宽度/高度,就可以玩家移动。例如:
if (Gdx.input.isKeyJustPressed(Input.Keys.A))
player.position.x = player.position.x-moveSpeed*deltaTime;
工作准确,但我希望它更平滑,在一秒内移动精确的距离,但每毫秒一点,而不是一次完全。
这使得玩家移动顺畅,但不准确:
{{1}}
如何进行平滑过渡,但是精确?
答案 0 :(得分:0)
更改(或扩展)您的播放器类,以便它包含以下内容:
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);