Game Development Reference
In-Depth Information
You probably already guessed that this is not going to be much fun as you have
to worry about all the housekeeping to load, reload, and unload while using lots
of assets, especially as these actions will add up very quickly with each new asset
that will be used. This is one of the reasons why LibGDX provides a manager class
for this task, which is called AssetManager . It allows you to delegate the work
of keeping a list of the loaded assets to the manager. The manager is also able to
asynchronously load new assets, which simply means that loading is done in the
background and therefore does not stop the updating and rendering to the screen.
This is a very useful functionality; for example, it allows you to render and update a
progress bar that shows the current loading status. Nonetheless, the actual loading of
our assets still has to be done on our own. For this reason, we are going to create our
own Assets class, which will also help us structure our loaded assets in logical units
and make them accessible from everywhere in the game code.
Organizing the assets
We will now create our own Assets class to organize and structure our assets. First,
add a new constant to the Constants class that points to the description file of the
texture atlas:
public class Constants {
// Visible game world is 5 meters wide
public static final float VIEWPORT_WIDTH = 5.0f;
// Visible game world is 5 meters tall
public static final float VIEWPORT_HEIGHT = 5.0f;
// Location of description file for texture atlas
public static final String TEXTURE_ATLAS_OBJECTS =
"images/canyonbunny.pack";
}
Next, create a new file for the Assets class and add the following code:
package com.packtpub.libgdx.canyonbunny.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
import com.packtpub.libgdx.canyonbunny.util.Constants;
public class Assets implements Disposable, AssetErrorListener {
 
Search WWH ::




Custom Search