Asset Manager

Posted

1. Asset Manager

1) Asset Manager

The asset manager in libgdx is an easy way to load all of your assets (images, sounds, etc) needed for your game. So first you need to create the AssetMgr class. Once you've done this, then you create the assetMgr object like this:

assetMgr = new AssetMgr();

Now in your assetMgr you'll first create a new AssetManager. Here's the line for that:

manager = new AssetManager();

Now when you want to load an image, for example, you'll provide the path in the assets folder and load it. So to load the file "badlogic.jpg" from the path "assets/img" you would use the following line:

manager.load("img/badlogic.jpg", Texture.class);

That would load that image into the manager. To load a texture packed image into the asset manager from the path "assets/packs", you would do the following:

manager.load("packs/anim.pack", TextureAtlas.class);

To find out more about texture packed images, check out my Texture Packer tutorial.

Sounds are loaded in a similar way. So to load a sound called "ding.wav" from the "assets/audio" dir, you would use the following line:

manager.load("audio/ding.wav", Sound.class);

Those lines will queue the assets, but not actually load them. This is because you may want to pre load some assets and not others, as in the case of a loading screen where you pre load the loading screen images so it can be shown while all of the other assets load. To force a load of all of the queued assets, you use the following line:

manager.finishLoading();

This will stop the processing and load all of the queued assets until they finish loading.

Once we have all of the images and sounds loaded, we can get them pretty easily. So if we want to retrieve an image, we do the following:

Texture img; img = manager.get("img/myImg.png",Texture.class);

That will load the image at "img/myImg.png" into the img var. You can also use this to load texture atlases and sounds in addition to other types using this format. So, for example, to get a sound, you would do the following:

Sound audio; audio = manager.get("audio/ding.wav",Sound.class);

That's really it for the basics of using the asset manager in libgdx. Now for my games, I use a slightly more complex asset manager class to make things easier when I'm programming. Because I typically put images and sounds into specific directories I create helper methods to make the loading and getting of assets simpler. So, for loading assets, I do the following:

manager.load(imgPath+"/"+file, Texture.class);

imgPath is a var I set in the constructor of my asset manager class, and file is the name of the image to be loaded. So now that I have this line, I create an entire method that looks like this:

public void loadImages() { for(String file : imgFiles) { manager.load(imgPath+"/"+file, Texture.class); } // END for } // END loadImages

Now when I call this method, it will load all images that are in the array "imgFiles". Finally, I add files to the "imgFiles" array like this:

imgFiles.add("walk_up.png"); imgFiles.add("walk_down.png"); imgFiles.add("walk_left.png"); imgFiles.add("walk_right.png"); imgFiles.add("crystal.png"); imgFiles.add("light_glow_effect.png");

This is all I do when I want to add a file. Simply add it in this list and it gets added to the asset manager! Now there is another image that I'm using in my BasicGame that I don't have listed here, and that's "badlogic.jpg". It isn't in that list because I'm showing it as soon as the game loads in the loadingScreen. As a result, I use an additional method which is below:

public void loadInitialImages() { for(String file : initialImgFiles) { manager.load(imgPath+"/"+file, Texture.class); } // END for } // END loadInitialImages

This method basically does the same thing as "loadImages", except that it only loads the images necessary to display on the loading screen. Here's the code for adding to the array:

initialImgFiles.add("badlogic.jpg");

Just add those files to a separate array, and we're off! I wrote a similar method for loading sounds. Here's the entire method, which looks similar to the other methods here:

public void loadSound() { for(String file : soundFiles) { manager.load(soundPath+"/"+file, Sound.class); } // END for } // END loadSound