Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly load textures using the libgdx assetmanager

Is this how one properly loads a texture into an AssetManager?

Texture tex;
AssetManager manager = new AssetManager();
manager.load("menu/bg.png",Texture.class);
tex = manager.get("menu/bg.png",Texture.class);

The texture fails to load with error "cannot load the texture menu/bg.png".

How should we load our texture using the AssetManager?

like image 976
J.Praveen Benjamin Avatar asked Sep 12 '12 15:09

J.Praveen Benjamin


1 Answers

This is almost how the AssetManager should be used, but not entirely. I recommend reading on the wiki about the libgdx AssetManager.

Some points:
The variable should be in camelCase, so AssetManager manager... instead of AssetManager Manager.

You will need to call manager.update(); to actually make it load stuff. This will need to be called until manager.update(); returns true, then it's done loading. So you can make a loading screen where you call manager.update(); each frame, and when it returns true you switch to some other screen.
If you just want everything to load, and block until it's loaded, call manager.finishLoading(); before trying to get anything from the manager.

You may have to create a folder in the assets folder named data and put your assets there instead of putting them directly in the assets folder. So put your assets in mygame-android\assets\data instead of mygame-android\assets.

If you're using the gdx-setup-gui to create your project, you should be fine. But if not, your desktop project will need to know where to find the assets as well.

An example on how to create an animated, responsive loading screen with libgdx. (video)

like image 66
Matsemann Avatar answered Nov 17 '22 08:11

Matsemann