Libgdx游戏每秒加速并放慢速度

时间:2016-05-17 18:59:13

标签: libgdx

我的游戏是一个平台游戏,屏幕跟随着不断向后移动的玩家。每一秒游戏都会减慢运动速度,然后再次加速。我怎么能阻止这种情况发生。我已经将delta时间传递到了我的world.step但是它们没有帮助延迟或口吃。感谢

MAIN RENDER LOOP

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

1 个答案:

答案 0 :(得分:1)

你没有写任何有关你的世界和身体设置的内容,但我想以下情况会发生:

  • 你对mario =&gt;施加冲动他的速度增加
  • 在接下来的几个世界步骤中,身体摩擦使马里奥减速
  • 只要b2Body.getLinearVelocity().x < 4成立,你就再次向马里奥施加冲动
  • 摩擦让他再次放慢速度
  • ...

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