LibGdx:Shaperenderer不在屏幕上绘制

时间:2017-07-03 18:08:43

标签: java libgdx

尝试创建一个简单的加载屏幕。下面的代码打印出正确的progress,所以我知道该部分有效。但矩形没有绘制。不确定是什么问题。

Full LoadingScreen:

public class LoadingScreen implements Screen {

  private static final float PROGRESS_BAR_WIDTH = MyGdxGame.WIDTH / 2f;
  private static final float PROGRESS_BAR_HEIGHT = 50f;

  GdxAssetManager assetManager;
  Stage stage;
  //Table mainTable;
  private ShapeRenderer shapeRenderer;
  private MyGdxGame game;

  public LoadingScreen(MyGdxGame game){
      this.game = game;
      assetManager = game.getAssetManager();
      shapeRenderer = new ShapeRenderer();
      stage = new Stage(new StretchViewport(MyGdxGame.WIDTH, MyGdxGame.HEIGHT));
  }

  @Override
  public void show() {
      assetManager.loadGeneral();
  }

  @Override
  public void render(float delta) {
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      renderProgressBar();

      if (assetManager.getManager().update()) {
          game.setScreen(new LoginScreen(game));
      }

      stage.act(Gdx.graphics.getDeltaTime());
      stage.draw();
  }

  private void renderProgressBar() {
      float progress = assetManager.getManager().getProgress();
      System.out.println(PROGRESS_BAR_WIDTH * progress);
      shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
      shapeRenderer.setColor(Color.RED);
      shapeRenderer.rect(
            (MyGdxGame.WIDTH - PROGRESS_BAR_WIDTH) / 2f,
            (MyGdxGame.HEIGHT - PROGRESS_BAR_HEIGHT) / 2f,
            PROGRESS_BAR_WIDTH * progress,
            PROGRESS_BAR_HEIGHT
      );
      shapeRenderer.end();
  }

  @Override
  public void resize(int width, int height) {
      stage.getViewport().update(width, height);
  }

  @Override
  public void pause() {

  }

  @Override
  public void resume() {

  }

  @Override
  public void hide() {
      dispose();
  }

  @Override
  public void dispose() {
      stage.dispose();
      shapeRenderer.dispose();
  }
}

我猜感兴趣的方法是renderrenderProgressBar。就像我说的,我得到的只是白色背景,直到加载完成,但renderProgressBar内的打印输出正确的值。

1 个答案:

答案 0 :(得分:2)

使用舞台相机设置ShapeRenderer的projectionMatrix。

shapeRenderer.setProjectionMatrix(stage.getCamera().combined);