2017-07-03 73 views
2

試圖創建一個簡單的加載屏幕。下面的代碼打印正確的progress,所以我知道該部分的作品。但矩形沒有被繪製。不知道什麼是錯的。LibGdx:在屏幕上未繪製的Shaperenderer矩形

完全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內的打印輸出正確的值。使用臺架相機的ShapeRenderer

回答

2

集projectionMatrix。

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

Bah,ofc!知道我錯過了一些小事。需要在'Render()'中設置,對嗎? –

+0

你可以調用渲染方法 – Aryan

+0

你有舞臺上的任何東西嗎?如果是在'stage.draw()'後面調用'renderProgressBar()' – Aryan