Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation in libgdx

Tags:

android

libgdx

I am wondering whether there is an easy way to animate in libgdx. I am thinking about an API where I can just specify the duration and the ending location of the texture (just like in Flash's Tween library).

I searched on google and the closest thing that I got was the Animation class that libgdx has which, I believe, requires the user to specify a texture for each keyframe.

new Animation(0.5f, texture1, texture2, texture3);
like image 313
denniss Avatar asked Dec 12 '22 09:12

denniss


2 Answers

You can use TextureRegion#split(texture, tileWidth, tileHeight) to get a bunch of texture regions for a texture, if they are laid out in a grid. You can also use the TexturePacker on a bunch of loose images to pack them into a single texture, then load that with TextureAtlas. This has the benefit that it can strip whitespace from each frame for better packing, and can also do fancy stuff like aliasing (if two frames are identical, it will only be packed once). Name your frame images like anim_1.png, anim_2.png, etc and then you get can all the texture regions named "anim" as an ordered list based on the frame number.

Also note that the Animation class in libgdx is simplistic. Don't hesitate to write your own if it doesn't meet your needs. An animation class is only about 20 lines of code.

like image 132
NateS Avatar answered Jan 16 '23 11:01

NateS


It depends what kind of animation you want. If it's an animation based on different images, then yes, you should use the Animation class with different texture regions. However, if you want a animation based on movement interpolations, you should use a tweening library, like in Flash.

Libgdx has a built-in Interpolator helper which allows you to create simple interpolations with easing. If you want a complete tweening engine like Flash ones, have a look at the java Universal Tween Engine.

like image 21
Aurelien Ribon Avatar answered Jan 16 '23 09:01

Aurelien Ribon