1
\$\begingroup\$

My understanding is that you initialize the AssetManager as a non-static object in your main class and load the sprites you need...

    manager = new AssetManager();
    manager.load("sprite_sheet.txt", TextureAtlas.class);
    manager.finishLoading();

I then modified my abstract State class by adding AssetManager to the constructor...

 State(GameStateManager gsm, AssetManager manager) {
    this.gsm = gsm;
    this.manager = manager;

Now say I have 5 blocks in my sprite sheet called block0, block1... block5 would it be appropriate to create a BlockTextures class like this?

private static TextureAtlas textureAtlas;

public BlockTextures(AssetManager manager) {
    textureAtlas = manager.get("sprite_sheet.txt", TextureAtlas.class);
}

public static AtlasRegion get(int id) {
    return textureAtlas.findRegion("block" + id);
}

public static void dispose() {
    textureAtlas.dispose();
}

Where I call BlockTextures.dispose() in my Game.dispose() method?

public void dispose() {
    BlockTextures.dispose();
}

While this code seems to work fine I feel like I am not using AssetManager correctly.

\$\endgroup\$
1
\$\begingroup\$

While you certainly can do what you're doing I would suggest that you pass your AssetManager around yo the classes that need to load resources instead. So instead of doing BlockTextures.get(id); where you need to use the texture you should instead pass the AssetManager and do

// Store this variable 
TextureAtlas atlas = manager.get(name, TextureAtlas.class);

//Use the Atlas later as you would normally
atlas.whatever();

It is also crucial that you don't dispose individual assets using their dispose method. Instead dispose of the AssetManager or use the unload method of the AssetManager to unload a specific resource.

You can read more on AssetManager on the wiki.

\$\endgroup\$
  • \$\begingroup\$ If I get a Texture from an TextureAtlas which I got from AssetManager I don't dispose of the Texture even though it's twice removed from the AssetManager? \$\endgroup\$ – java Jun 12 '17 at 21:43
  • \$\begingroup\$ @java You either call manager.unload(resource) or manager.dispose(). You never call dispose on the actual resource. \$\endgroup\$ – Charanor Jun 12 '17 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.