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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With