3

I managed to pack a textureatlas with images i have and it works properly in that it creates the .pack file and the .png file. The issue is when i load the texture atlas and try to assign AtlasRegions. it loads the entire atlas instead of only the image i want. here is a mockup code i used to test it.

@Override
public void create() {      
    camera = new OrthographicCamera(800, 480);
    batch = new SpriteBatch();

    test = new TextureAtlas(Gdx.files.internal("polytest.png"));

    sprite = test.findRegion("hero");
    texture = new Texture(Gdx.files.internal("data/libgdx.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);


}

@Override
public void render() {      
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(sprite.getTexture(), 10, 10);
    batch.end();
}

this renders the entire atlas instead of only the image called "hero" in the atlas. Why does this happen?

Thanks in advance.

Shadow
  • 421
  • 3
  • 14

1 Answers1

8

When you do.-

batch.draw(sprite.getTexture(), 10, 10);

you're actually telling libgdx to render the whole Texture. What you need is to draw just the TextureRegion.-

batch.draw(sprite, 10, 10);
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • Thank you so much, i have been slaving over this whole weekend! cant believe it was so simple :O – Shadow Nov 04 '13 at 18:21