Search code examples
libgdx

How can I change the texture in a Scene2D Image using 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.


Solution

  • 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)));