How to finish animation with libgdx

I'm trying to implement simple animation using libGDX, and currently I'm stuck in one thing. Let's say I have a bunch of sprites that take time. For example, about 30 sprites such as this link: https://github.com/libgdx/libgdx/wiki/2D-Animation

However, before the animation is completed, a key is pressed. For smooth animation, I want 30 frames to be filled before I start the next set of animations to prevent a sudden stop.

So my question is, how can I achieve this in libGDX? My current idea is to extend the Animation class, which will track how I have frames and how many were displayed, and then display the rest. Or use the isAnimationFinished(float stateTime) function (although I had no luck with this).

The examples that I saw as a super jumper have very few animations and do not change very much.

Also, is there a way to keep a list of sprites from the TextureAtlas.createSprites method and use them with the Animation class? If not, then why is this feature needed?

thanks

+4
source share
3 answers

you can use

 animation.isAnimationFinished(stateTime); 

To find out if your animation is complete.

For sprites: personnaly I use a TextureRegion from TextureAtlas and I store them in an array for my animation

+5
source

I am creating an AnimatedImage class that extends Image to automate writing in Image. My code would be like this:

 public class AnimatedImage extends Image{ private Array<Array<Sprite>> spriteCollection; private TextureRegionDrawable drawableSprite; private Animation _animation; private boolean isLooping; private float stateTime; private float currentTime; public AnimatedImage(Array<Array<Sprite>> _sprites, float animTime, boolean _looping){ // set the first sprite as the initial drawable super(_sprites.first().first()); spriteCollection = _sprites; // set first collection of sprite to be the animation stateTime = animTime; currentTime = 0; _animation = new Animation(stateTime, spriteCollection.first()); // set if the anmation needs looping isLooping = _looping; drawableSprite = new TextureRegionDrawable(_animation.getKeyFrame(currentTime)); this.setDrawable(drawableSprite); } public void update(float delta){ currentTime += delta; TextureRegion currentSprite = _animation.getKeyFrame(currentTime, isLooping); drawableSprite.setRegion(currentSprite); } public void changeToSequence(int seq){ // reset current animation time resetTime(); _animation = new Animation(stateTime, spriteCollection.get(seq)); } public void changeToSequence(float newseqTime, int seq){ _animation = new Animation(newseqTime, spriteCollection.get(seq)); } public void setRepeated(boolean _repeat){ isLooping = _repeat; } public boolean isAnimationFinished(){ return _animation.isAnimationFinished(currentTime); } public void resetTime(){ currentTime = 0; } } 

changetosequence method will make a new Animation , which will be used to update the current TextureRegionDrawable using the update method. resetTime will reset the total animation time when calling changetosequence . You can add an event listener to call the changeToSequence method.

Here is an example:

 private AnimatedImage _img; 

then I add an InputListener as follows:

 _img.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ _img.changeToSequence(1); return true; } }); 

Hope this helps.

+2
source

Use the animation engine for this type of animation. Its well documented and libgdx supports it. Google is about this, and you can find a bunch of examples using libgdx .. Hope this helps you!

+2
source

Source: https://habr.com/ru/post/1447406/


All Articles