Textures loaded using AssetManager do not appear on screen

Hi friends!

I’m working on a breakout clone to learn game development basics and have strange trouble with loading resources. Loading fonts from “.ttf” files works and I can use them without any problems. Looks like textures are loaded successfully (no exceptions), but when I draw a texture, It can’t be seen on a screen, in other words, the screen is black. At the same time, when I load the texture using Gdx.files.internal(“bla/bla.png”), it is displayed on the screen. What the hell?

Here goes some code, don’t know if it is enough.

Loading textures via AssetManager:


assetManager.load(BALL, Texture.class);
assetManager.load(BRICK, Texture.class);
assetManager.load(PADDLE, Texture.class);
assetManager.finishLoading();

// Then I load some fonts

Constructing sprites:


ball = new Sprite(assetManager.get(BALL, Texture.class));
brick = new Sprite(assetManager.get(BRICK, Texture.class)); //BRICK == "textures/brick.png"
paddle = new Sprite(assetManager.get(PADDLE, Texture.class));

Both ways do not work:


brick.setPosition(10, 10);
brick.draw(batch);

batch.draw(brick.getTexture(), 2, 2);

BUT if I load brick’s texture like this, it’s being properly displayed:


brick = new Sprite(new Texture(Gdx.files.internal("textures/brick.png")));

That’s it… I’ll continue development using Gdx.files.internal(…) just because it doesn’t really matter that much, I need just three textures in a single class (GameScreen). But I’d like to know how to deal with this issue to write as good code as possible for a simple game and to get advantage of AssetManager for a complex one with large number of different textures.

Hey @mmx358,

I can’t see anything wrong with the code you provided, I suspect it may be down to when you are loading and assigning the images.

You can check if the images have loaded with

if(manager.isLoaded("data/mytexture.png")) {
    // image loaded to set it as sprite
}

[quote]But I’d like to know how to deal with this issue to write as good code as possible for a simple game and to get advantage of AssetManager for a complex one with large number of different textures.
[/quote]
I have a guide on my blog for the Asset Manager which can be used for more than just textures. As well as the Texture packer which is a great tool for packing lots of images into a single atlas. These are great when used together as it allows you to have a pack for your loading screen, a pack for your game assets and a pack for GUI etc which can be loaded or unloaded separately.

I very much doubt it’ll make a difference (I haven’t looked at the source), but the get() method on AssetManager doesn’t have to take a class type. I can’t see why not including it would help, although I also can’t see why your code wouldn’t work - it’s pretty much the same as I’ve got in my Asset loading class…

I also second the recommendation to use TexturePacker (although I’d suggest the newer one.)

dfour, isLoaded returns true in my case. :-\

Thank You for this advice, I’ll try that during “polishing phase”.

I very much doubt it’ll make a difference (I haven’t looked at the source), but the get() method on AssetManager doesn’t have to take a class type. I can’t see why not including it would help, although I also can’t see why your code wouldn’t work - it’s pretty much the same as I’ve got in my Asset loading class…

I also second the recommendation to use TexturePacker (although I’d suggest the newer one.)

I use it because without class type it should be explicitly cast to a Texture class, which is “dirty style” in my opinion… Anyway, this does not help.

Okay, thank You too, I’ll definitely incorporate this technique after some time.

I think I should do some “experiments” with different order of loading assets, different texture image formats and so on. Probably this will happen few days later because of other things to do.