My use case is slightly different, but I was running into the same issue.
I have an object, which extends Actor
, that I want to draw using a region in an atlas.
public class MyObject extends Actor {
private Texture texture;
public MyObject() {
TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
texture = textureAtlas.findRegion("objectTexture").getTexture();
Gdx.app.log(TAG, "Texture width: " + texture.getWidth());
Gdx.app.log(TAG, "Texture height: " + texture.getHeight());
setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
// ...
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture,
worldPosition.x * Constants.STANDARD_TILE_WIDTH, worldPosition.y * Constants.STANDARD_TILE_WIDTH,
texture.getWidth(), texture.getHeight());
}
}
Instead of just the region I was expecting I instead received the entire atlas, so my logged texture width and height were 1024 x 128.
Unfortunate, and still not sure why getTexture()
returns too much, but switching over to batchDraw(TextureRegion, ...)
at least got me in a better place.
public class MyObject extends Actor {
private TextureRegion texture;
public MyObject() {
TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
texture = textureAtlas.findRegion("objectTexture");
setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
// ...
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture, getX(), getY());
}
}
Based upon what I saw with my sprites, the questioner was seeing a blue square for the same reason I was; the entire image is getting loaded by getTexture()
and since it starts at the bottom left, you're always seeing a blue square.
Using the Sprite(TextureRegion)
constructor may have also resolved their issue.