Search code examples
javalibgdx

Libgdx multiple textures in one sprite


I'm working for a game and got into this point: I would like to have 2 textures in a single sprite, is that possible?

I mean: I need one to be set in a specific angle, and the other one to be rotating continuously. I've been looking on the internet but found nothing related.

Or.. is there a better way of doing this?


Solution

  • A Sprite inherits from a TextureRegion. This is the definition of TextureRegion:

    Defines a rectangular area of a texture.

    So a Sprite really is just a piece of Texture, that means you can only transform a Sprite as a whole.

    If you want to wrap multiple Sprites into a single class, I suggest using Scene2D. You could draw two sprites in Actor.draw():

    public void draw() {
        sprite1.draw();
        sprite1.rotate(…);
        …
        sprite2.draw();
        …
    }
    

    The actor is then added to a Stage

    stage.addActor(actor);