Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the texture in a Scene2D Image using libGDX?

Tags:

libgdx

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.

like image 356
Voldemort Avatar asked Feb 01 '14 07:02

Voldemort


2 Answers

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)));
like image 69
noone Avatar answered Nov 14 '22 05:11

noone


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.

like image 37
jefferson_amarals Avatar answered Nov 14 '22 03:11

jefferson_amarals