简体   繁体   中英

AssetManager libgdx Asset not loaded

I am having an issue with my AssetManager in libgdx, I am creating it in my Main class and have a getMethod to return the assetManager to my screens. When I go to assetManager.get(etc.) in my screens classes It says

FATAL EXCEPTION: GLThread 563
                  Process: com.johnny.gamerpg, PID: 9855
                  com.badlogic.gdx.utils.GdxRuntimeException: 
                    Asset not loaded: data/backgrounds/mainMenu.png

This is my create() in the Main Class.

public void create()
{

    batch = new SpriteBatch();
    assetManager = new AssetManager();
    assetManager.load("data/backgrounds/mainMenu.png", Texture.class);

    startTime = TimeUtils.millis();
    this.setScreen(new Splash(this));


    if(assetManager.update() && TimeUtils.timeSinceMillis(startTime) > 3000)
    {
        setMainMenuScreen();
    }
}

This is my MainMenu class constructor.

public MainMenu(GameControl gam)
{
    this.game = gam;
    assetManager = gam.getAssestManager();

    background = assetManager.get("data/backgrounds/mainMenu.png", Texture.class);

}

setMainMenuScreen()

public void setMainMenuScreen()
{
    setScreen(new MainMenu(this));
}

Do you ever get to the menu screen? you switch to Splash() before assetmanager is done loading and before the 3000 milisecs. Even before you check for any of those.

assetManager.update() will return false until the asset is loaded. assetManager.update() is meant to be called every frame until it returns true. This means that you should check for

if(assetManager.update()){
    //done loading
}

in your render()

You should start with moving

if(assetManager.update() && TimeUtils.timeSinceMillis(startTime) > 3000)
{
    setMainMenuScreen();
}

into render() and rethink how and when you switch to the Splash screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM