首页 文章

libgdx,特定于运动

提问于
浏览
1

我对运动有疑问 . 我的目标是在按下键 for its own width/height 后按 player move . 例如:

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;

如何进行平滑过渡,但是精确?

1 回答

  • 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);
    

相关问题