[英]libgdx, movement specific
我有运动问题。 我的目标是在按下按键以使其自己的宽度/高度时让玩家移动 。 例如:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.