Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx: AssetManager not loading assets

Okey Im simply struggling with the AssetManager in libgdx. I´ve followed every tutorial, read every wikipage, but I cant get it to work.

Asset class:

public class Assets {

private static final AssetManager manager = new AssetManager();
public static final String background = "data/lawn.png";
public static void load() {

    manager.load(background, Texture.class);

}
public static void dispose()     {
    manager.dispose();
}

public static boolean update() {
    return manager.update();
}

Main class:

public class TombStone extends Game implements Screen {
@Override
public void create () {

    Assets.manager.update();



}

And I call my textures like this in a Screen class:

public class StoneScreen implements Screen{
public Texture texture;
public StoneScreen(TombStone gam){
loadStandard();
}
public void loadStandard() {
texture= Assets.manager.get(Assets.background, Texture.class);
}

So when I run the app it crashes before showing anything giving me: "FATAL EXCEPTION: GLThread 32182" "Asset not loaded: assets/data/lawn.png"

like image 749
Benni Avatar asked Apr 03 '15 01:04

Benni


1 Answers

There's so much wrong with your code...

public class Assets {

public static final AssetManager manager = new AssetManager(); // 1)
public static final String background = "assets/data/lawn.png"; // 2)
public static void load() {

    manager.load(background, Texture.class);
    manager.get(background, Texture.class); // 3)
}
public static void dispose()     {
    manager.dispose();
}

public static boolean update() {
    return manager.update();
}

public class TombStone extends Game implements Screen { // 4)
@Override
public void render () {
    //assets.manager.update();
    Assets.load(); // 5)
    Assets.manager.update();
    super.render();
}

1) Your manager is public, but you still offer other methods to manipulate it (for example update and dispose). Make it private or remove the methods and directly work on Assets.manager.

2) Assets are relative to the assets directory. That's why it should be data/lawn.png.

3) This is why you are getting the exception. AssetManager.load() just marks something to be loaded, but doesn't instantly load it yet. This is what either AssetManager.update() (asynchronous) or AssetManager.finishLoading() (synchronous) does. If you try to use AssetManager.get() before actually loading it, you get this exception. Simply remove this line.

4) It does not make sense to extend Game AND implement Screen. Do only one of them at a time. A Game has Screens, but it isn't a Screen itself.

5) You are marking your assets to be loaded over and over again. render() is called every frame. Move Assets.load() to the Screen.show() method, which is executed only once.

like image 175
noone Avatar answered Nov 12 '22 03:11

noone