0

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.

  • In first aproach you are rendering AtlasRegion in second one TextureRegion. Im not sure those are the same. Huh? – Veljko Aug 20 '14 at 06:39
  • 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 TextureRegion`](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureAtlas.AtlasRegion.html) – kajacx Aug 20 '14 at 07:46
  • No, sorry, that's not the issue, I have tried different indices.....hmmmm. – drew tenenbaum Aug 20 '14 at 18:02

2 Answers2

0

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;
    }
}
Community
  • 1
  • 1
lxknvlk
  • 2,744
  • 1
  • 27
  • 32
  • Thanks, but sorry, but that's not what I'm trying to do. I'm not trying to draw an array of rose objects, I'm trying to draw a single Rose Object that has an array of textureRegions with an index to specify which to draw on each call. The Rose class has an array of textureRegions that represents each frame of an animation. – drew tenenbaum Aug 20 '14 at 18:03
  • Well now what you want is clearer to understand, you should have written that in your question. You could write custom getTextureRegion(int i) method that chooses and returns the texture region dependent on the value you passed to it, and you will call it in batch.draw(rose.getTextureRegion(1),x,y); Ill update my answer. – lxknvlk Aug 20 '14 at 19:06
  • Sorry You're right, I should have included that. Also what your provided is pretty much exactly exactly what I'm doing. Look at my code. The issue isn't that I don't know how to structure it, instead is that it seems as though caching the TexureRegion upfront, then passing it into the object will draw nothing, while initializing it inside the object will draw it. Not only that, but I can trace the properties of the passed in texture (height, width, name, etc...), but when I draw it nothing will appear. This is a case of unexpected behavior, not a "how do i do this." – drew tenenbaum Aug 20 '14 at 19:15
  • Strange, it should work. I see no reason for it not to work. A TextureRegion is a TextureRegion, no matter when you initialize it. Maybe you can update your answer with some more code? I find it strange that you pass a .txt file into the Atlas. Also, have you checked in the .pack file that your frames really have the correct index?You know that you have to name the image files like name_index.png, eg rose_01.png. Then 01 goes to index. – lxknvlk Aug 20 '14 at 19:43
  • Right. That's why I'm confused. When I get home, I'll post more of my code, if I can, but The naming is not the issue, nor is the text file. because the same code findRegion("Dirt", 0) works in the draw() method of Rose, but not if i use it and then pass the found region in. – drew tenenbaum Aug 20 '14 at 20:01
0

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.