Search code examples
libgdx

How to use Amination in LibGDX


So when I try something like this

batch.draw(animation.getKeyFrame(timeEllapsed, true), 0, 0);

I get an error because draw only takes a Texture or Texture Region Object, but animation.getKeyFrame(float, boolean) returns an Object, and can't be cast to either of those other Objects. I am making a game for an OOP class, but I can't find any current tutorials on LibGdx and the framework has seemed to change since a few years ago.

Thank you


Solution

  • This is a recent change in LibGDX, so tutorials may be outdated.

    Animation is now generic so it can support animation frame types of any kind, not just TextureRegions.

    To use it with TextureRegions, declare the type:

    public Animation<TextureRegion> myAnimation;
    
    //...
    
    myAnimation = new Animation<TextureRegion>(/*...*/);