Libgdx: ui asset management block screen

I am using AssetManager

to load all assets into my game. I have a problem displaying the loading progress or, conversely, progress. I am trying to set just the background color using this code in the LoadingScreen render method.

    Gdx.gl.glClearColor(0.431f, 0.792f, 0.808f, 0xff / 255.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      

Everiting loads well, but when loading I get a black screen instead of the color set in glClearColor. The same happens when implementing a loading bar, for example this example

Here's the Assests

class part

    public class Assets implements Disposable, AssetErrorListener {

        public static final Assets instance = new Assets();
      .
      .
        public void init (AssetManager assetManager) {
            this.assetManager = assetManager;
        // set asset manager error handler
        assetManager.setErrorListener(this);
        // load texture atlas
        assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
     .
     .
        TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
     .


   }  
     .


}

      

Here is a simple code of mine LoadingScreen

:

    @Override
    public void show() {
        manager = new AssetManager();

        Assets.instance.init(manager);
   }

    public void render(float deltaTime) {
        Gdx.gl.glClearColor(0.431f, 0.792f, 0.808f, 0xff / 255.0f);//light blue
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        if (manager.update()) {
            ScreenTransition transition = ScreenTransitionFade.init(0.5f);
            game.setScreen(new HomeScreen(game), transition);
        }
}

      

Here I was expecting a blue blank screen to show when loading assets, but I am getting a black screen.

I am assuming that some code is blocking the main thread, but I do not know how to fix this.

thanks for the help

+3


source to share


1 answer


Make sure you know when Screen.show()

and Screen.render(float delta)

:

Screen.show()

: Called when called Game.setScreen()

.

Screen.render(float delta)

: called continuous after completion Screen.show()

. Consequently, the download Screen.show()

is not finished yet and the default background (black) is displayed.



To avoid that a separate thread would be nice. Here goes AssetManager

, which loads things asynchronously. Now we only use it correctly:

Call AssetManager.load(<your assets>)

up , you call Game.setScreen(<Loading Screen>)

. LoadingScreen.render(float delta)

then called directly and you see your background. To check if the download is complete, you can use AssetManager.update()

that returns true if done and false else.

0


source







All Articles