【问题标题】:How to finish animation with libgdx如何使用 libgdx 完成动画
【发布时间】:2012-11-21 05:52:07
【问题描述】:

我正在尝试使用 libGDX 实现一个简单的动画,但我目前正忙于一件事。可以说我有一堆精灵需要一些时间才能完成。例如,大约 30 个精灵像这个链接:https://github.com/libgdx/libgdx/wiki/2D-Animation

但是,在动画完成之前,某个键被按下。对于流畅的动画,我希望在开始下一组动画之前完成 30 帧,以防止突然停止。

所以我的问题是如何在 libGDX 中实现它?我目前的想法是扩展Animation 类,它会跟踪我有多少帧以及渲染了多少帧,然后显示其余的。或者使用isAnimationFinished(float stateTime) 函数(虽然我没有运气使用它)。

我见过的像 superjumper 这样的例子只有很少的动画,也没有太大的变化。

另外,有没有办法保存来自TextureAtlas.createSprites 方法的精灵列表并将它们与动画类一起使用?如果没有,提供这个功能的目的是什么?

谢谢

【问题讨论】:

  • 您能否提供更多有关您如何使用动画的背景信息? isAnimationFinished(float stateTime) 非常简单,可能是您使用错误。您如何检测动画何时“正常”结束?

标签: libgdx


【解决方案1】:

你可以使用

animation.isAnimationFinished(stateTime);

查看您的动画是否完成。

对于精灵:我个人使用 TextureAtlas 中的 TextureRegion 并将它们存储在一个数组中用于我的动画

【讨论】:

    【解决方案2】:

    我创建了一个扩展 Image 的类 AnimatedImage 以在 Image 中自动生成精灵。我的代码会是这样的:

    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 方法将生成新的Animation,用于在update 方法中更新当前的TextureRegionDrawable。当您调用changeToSequence 时,resetTime 将重置动画的总时间。您可以添加事件侦听器来调用 changeToSequence 方法。

    示例如下:

    private AnimatedImage _img;
    

    然后我像这样添加 InputListener:

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

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      对这种动画使用补间引擎。它有据可查,libgdx 支持它.. 谷歌关于它,你可以找到一堆使用 libgdx 的例子.. 希望它会帮助你!

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2019-01-24
        • 2018-11-06
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多