【问题标题】:Libgdx game speeding up and slowing down every secondLibgdx 游戏每秒加速和减速
【发布时间】:2016-05-17 18:59:13
【问题描述】:

我的游戏是一个平台游戏,屏幕会跟随不断在背景中移动的玩家。游戏每秒都会减慢向右的移动速度,然后再次加速。我将如何阻止这种情况发生。我已经通过 delta time 进入我的 world.step 但他们没有帮助滞后或口吃。谢谢

主渲染循环

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

【问题讨论】:

  • 我不确定你的 world.step() 到底在做什么,但这听起来像是那种可能也需要增量时间的函数

标签: libgdx


【解决方案1】:

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

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

除此之外,你绝对应该将 deltaTime 传递给世界的 step 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2012-03-03
    相关资源
    最近更新 更多