Libgdx游戏每秒钟都在加速和减速



我的游戏是一个平台游戏,屏幕跟随不断向右移动的玩家。游戏每一秒都会减慢移动速度,然后再次加快。我该如何阻止这种情况的发生。我已经把delta时间带入了我的世界。step,但它们对滞后或口吃没有帮助。感谢

主RENDER循环

   public void update(float dt){
        world.step(1 / 60f, 2, 2);
        handleInput(dt);
        camX();
        player.moveMario();
        hud.update();
        gameCam.update();
        renderer.setView(gameCam);
    }
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();
        game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();
        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();
        game.batch.draw(ball, player.b2Body.getPosition().x - MarioBros.RADIUS_CHARACTER / MarioBros.PPM, player.b2Body.getPosition().y - MarioBros.RADIUS_CHARACTER / MarioBros.PPM, 70 / MarioBros.PPM, 70 / MarioBros.PPM);
        mapCreator.createSpikeBalls(map, animation, game.batch);
        endOfGame();
        game.batch.end();
        b2dr.render(world, gameCam.combined);
    }
    public void camX(){
            gameCam.position.x = gamePort.getWorldWidth() / 2;
            if(player.b2Body.getPosition().x  >= gamePort.getWorldWidth() / 2){
                gameCam.position.x  = player.b2Body.getPosition().x;
            }
    }

将角色移动到右侧

 public void moveMario(){
        if (b2Body.getLinearVelocity().x < 4){
            b2Body.applyLinearImpulse(new Vector2(2.7f, 0), b2Body.getWorldCenter(), true);
        }
    }

你没有写任何关于你的世界和身体设置的东西,但我猜会发生以下情况:

  • 你对马里奥施加了一种冲动=>他的速度增加了
  • 在接下来的几个世界步骤中,身体摩擦减缓了mario的速度
  • 一旦b2Body.getLinearVelocity().x < 4为真,你就会再次对mario施加冲动
  • 摩擦使他又慢了下来

除此之外,您肯定应该将deltaTime传递给世界上的步长方法

最新更新