Java libgdx,特定于运动的
我对运动有意见。我的目标是,当按下键以获得自己的宽度/高度时,让玩家移动。例如:Java libgdx,特定于运动的,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))
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);
更改(或扩展)玩家类,使其包含以下内容:
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);
看看缓和tween函数,比如:线性tween可能是您想要的。还有其他tweening函数,但所有具有相同输入值(变量)的函数都可以查看缓和tween函数,如:线性tweening可能是您想要的。还有其他tweening函数,但都具有相同的输入值(变量)