Reputation: 969
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"
Upvotes: 6
Views: 7059
Reputation: 1067
the AssetManager can work in synchronous and asynchronous way.
Asynchronous: you show a loading a progress bar during which you load your asset. This is asynchronous because you load the assets in the background while you update the screen. You do this by calling the assetManager.update() over and over (as part of your game loop) until all asset are loaded. This allow you to update the progress bar.
This does mean that you cannot use the texture until you confirmed the assetManager has completed the load.
example below:
public MyAppListener implements ApplicationListener {
public void load(){
assetManager.load(t1, Texture.class);
assetManager.load(t2, Texture.class);
assetManager.load(t3, Texture.class);
}
public void render() {
if(manager.update()) {
// we are done loading, let's move to another screen!
}
// display loading information
float progress = manager.getProgress()
... left to the reader ...
}
}
You load all of your assets synchronously so you call sequentially, once (instead of in your game loop)
assetManager.load(t1, Texture.class);
assetManager.load(t2, Texture.class);
...
assetManager.load.load(tn, Texture.class);
then you call
assetManager.finishLoading();
The game will then wait until the assets are loaded before it continues, but it does mean that it will freeze your screen if you are loading a lot of stuff.
more info here: https://github.com/libgdx/libgdx/wiki/Managing-your-assets
Upvotes: 0
Reputation: 100
I know that this was asked (and partially answered) a while ago, but I'll give my fix. In your render method, put something like:
if (!assetManager.update()) return;
This should update the asset manager if it hasn't already been and stop you from rendering unloaded textures.
Upvotes: 0
Reputation: 19776
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.
Upvotes: 8