0
votes

I'm having some issues drawing TextureRegions with a spriteBatch in LibGDX.

So I have a main class that hosts the game logic. In the constructor, I have:

atlas = new TextureAtlas(Gdx.files.internal("sheet.txt") );
this.loadTileGFX();

the loadTileGFX() method does this:

roseFrames = new ArrayList<AtlasRegion>();

roseFrames.add(atlas.findRegion("Dirt", 0));
roseFrames.add(atlas.findRegion("Dirt", 1));
roseFrames.add(atlas.findRegion("Dirt", 2));
roseFrames.add(atlas.findRegion("Dirt", 3));

Then I pass the arrayList of AtlasRegions into the object:

///in the main class
rsoe = new RoseSquare(roseFrames, st, col, row, tileWidth);

//in the constructor for the object to draw
this.textureRegions = roseFrames;

Then every render() loop I call:

batch.begin();
rose.draw(batch);
batch.end()

The rose.draw() method looks like this:

public void draw(SpriteBatch batch){
    batch.draw(this.textureRegions.get(1), rect.x, rect.y, rect.width, rect.height);
}

But the thing is, this doesn't draw anything to the screen.

BUT HERE'S THE THING. If I change the code to be:

 public void draw(SpriteBatch batch){
    batch.draw(new TextureAtlas(Gdx.files.internal("sheet.txt")).findRegion("Dirt", 0)), rect.x, rect.y, rect.width, rect.height);
}

Then it draws correctly. Can anybody shed some light on what error I might have? Keep in ming I don't get any errors with the "nothing drawn" code. Also, I can trace the details of this.textureRegions.get(1), and they all are correct....

Thanks.

2
In first aproach you are rendering AtlasRegion in second one TextureRegion. Im not sure those are the same. Huh?Veljko
This is probably the problem, but you draw second texture in your first code (at index 1), but first texture in your second code (at index 0). Also @Veljko AtlasRegion extends TextureRegionkajacx
No, sorry, that's not the issue, I have tried different indices.....hmmmm.drew tenenbaum

2 Answers

0
votes

If you need to draw an array of something that has textures you can do like this:

batch.begin();
for (Ground ground : groundArray){
    batch.draw(ground.getTextureRegion(), ground.x, ground.y);
}
batch.end();

As you see i am drawing the TextureRegion here.

You can check related classes and other information in my answers HERE and HERE

Answering drew's comment:

public TextureRegion customGetTextureRegion(int i){
    switch(i){
    case 1:  return atlas.findRegion("dirt1"); break;
    case 2:  return atlas.findRegion("dirt2"); break;
    case 3:  return atlas.findRegion("dirt3"); break;
    }
}
0
votes

I have found a solution to my own problem.

I was also drawing some debug ShapeRenderer stuff.

The issue seemed to be that libGDX didn't like a SpriteBatch and a ShapeRenderer to be "on" at the same time:

//LibGDX Doesn't like this:
spriteBatch.begin();

shapeRenderer.begin(ShapeType.Line);
shapeRenderer.drawRect(x, y, width, height);
shapeRenderer.end();

sprtieBatch.draw(texRegion, x, y, width, height);
spriteBatch.end();

It prefers:

//LibGDX likes this:
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.drawRect(x, y, width, height);
shapeRenderer.end();


spriteBatch.begin();
sprtieBatch.draw(texRegion, x, y, width, height);
spriteBatch.end();

Thanks for your responses everyone.