Boldijar Paul
Boldijar Paul

Reputation: 5495

libgdx- changing the sprite texture with animation

I am using the animation class to make a simple animation, with only 2 frames. With animation, i can get the index, or texture region at the current time.

if (!animation.isAnimationFinished(time))
    time += Gdx.graphics.getDeltaTime();
else
    time = 0;

But the problem is, that I can't load any texture, its width and height must be a power of 2, so i got a big 1024x1024 texture, that has the 2 images. With sprite.setTexture() i can only put a Texture, but animation.getframe returns a texture region.

Is there a way to change the sprite texture with an animation?

Also

 sprite.setTexture(animation.getKeyFrame(time).getTexture());

Does not work.

This works

sprite = new Sprite(animation.getKeyFrame(time));

But I don't think is the best idea... I think is slow can can make bugs.

Upvotes: 3

Views: 6960

Answers (1)

Daahrien
Daahrien

Reputation: 10320

You usually use TextureRegion for sprites. The method that you are looking for is TextureRegion#setRegion (Sprite extends TextureRegion).

Like this:

sprite.setRegion(animation.getKeyFrame(time));

Upvotes: 6

Related Questions