10
votes

How can I change the texture in a Scene2D Image?

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Image.html

There is no such method in the docs. I create mine by providing its constructor a texture reference.

2

2 Answers

20
votes

You have to change the drawable and wrap your new texture in a SpriteDrawable

Texture texture = ...;
Image image = new Image(texture);

// switch to a new texture
Texture newTexture = ...;
image.setDrawable(new SpriteDrawable(new Sprite(newTexture)));
1
votes

You can use:

Texture texture = ...;
Image image = new Image(texture);

// change to a new texture
Texture newTexture = ...;
image.setDrawable(new TextureRegionDrawable(new TextureRegion(newTexture)));

Without use SpriteDrawable or create new Sprite instances.