Reputation: 17629
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);
Upvotes: 4
Views: 4964
Reputation: 53
Animating in libgdx is easy. What I do is I use an Array of texture or textureregion in the Animation parameter rather than putting many texture or textureregions. And after I declare I then set the animation's playmode like this --> animation.setPlayMode(Animation.PlayMode.LOOP);
it works . I draw the animation using the float runTime variable which is set like this runTime += delta or runTime = runTime + delta --> spriteBatch.draw(animation.getKeyFrame(runTime), x, y, width, height);
Upvotes: 0
Reputation: 7634
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.
Upvotes: 2
Reputation: 5876
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.
Upvotes: 11