So I’m still struggling with finding a good way to handle my assets.
I’ve looked at Dermatfan’s tutorial on AssetLoader, but i was wondering if there was an even better way to organize and use assets.
My game has over 200 assets and a whole bunch of different classes that continuously need to grab theses assets and do something with them and I’d prefer it if every time when I need to use an asset i wouldn’t need to get them like this:
Asset.manager.get(Assets.[i]TextureName[/i], Texture.class)
+ the additional coding i need to do to pass either the Assets class around or take care of accessibility
What i woud prefer is be able to just ONLY use TextureName whenever i want to use a texture and my knowing what texture I’m referring to.
One way to do that is is the STATIC keyword. And add an import statement on every class where i want to use my Assets like:
import static Assets.*;
Now my IDE automatically grabs the right assets from my Assets class:
Here is the problem:
I know the static keyword is not very popular amongst knowledgable programmers and for good reason.
One main reason is probably due to causing context loss issues on Android devices.
But i was thinking:
"now what if i created a class holding [public static TextureRegion] fields for pointing to all my assets, so i can access/get my assets just i describes above like this:
public class ImageAssets {
// player pack Textures
public static TextureRegion ship_middle, ... etc. etc.;
public ImageAssets() {
// player pack assests
//
ship_middle= AssetLoaderTest.res.getAtlas("player").findRegion("ship_middle");}
}
Have a class that let’s me load me Texture atlasses like this:
public class ResourceHandler {
private HashMap<String, TextureAtlas> atlases;
public ResourceHandler() {
atlases = new HashMap<String, TextureAtlas>();
}
public void loadAtlas(String path, String key) {
atlases.put(key, new TextureAtlas(Gdx.files.internal(path)));
}
public TextureAtlas getAtlas(String key) {
return atlases.get(key);
}
}
And now i could load my Assets from in my main class and can use the “import static class.*;” statement in every class to refer to the necessary assets.
And I assume because my assets get initialized in the create() i won’t have trouble with context loss anymore.
import static com.assets.ImageAssets.*;
public class AssetLoaderTest extends ApplicationAdapter {
SpriteBatch sb;
ImageAssets imgAssets;
public static ResourceHandler res;
@Override
public void create () {
sb = new SpriteBatch();
res = new ResourceHandler();
res.loadAtlas("player/player.pack", "player");
imgAssets = new ImageAssets();
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sb.begin();
sb.draw(ship_middle, 0, 0);
sb.end();
}
}
But before i start packing all my textures and coding my resource management like this I thought ask you guys what you think of it.
This code is working on my desktop. But will it cause any issues on Android/iOS devices.
What about the use of static keywords here.