在后台LibGDX中设置图像动画

在后台LibGDX中设置图像动画,libgdx,Libgdx,我需要一张在舞台中显示的动画图片,因为我不知道是否有任何方法可以建立它,但我想知道这是一种好方法还是有另一种更有效的方法可以在libgdx中创建。 分享如何采取我想做的事情: private class ImageAnimate extends Widget{ private Animation animation; private float animationTime; private int align = Align.center;

我需要一张在
舞台
中显示的动画图片,因为我不知道是否有任何方法可以建立它,但我想知道这是一种好方法还是有另一种更有效的方法可以在
libgdx
中创建。 分享如何采取我想做的事情:

private class ImageAnimate extends Widget{

        private Animation animation;
        private float animationTime;

        private int align = Align.center;

        private int imageX;
        private int imageY;

        /** Crea una imagen animada 
         * se asume que todos los frames de la animacion son de las misma dimensiones
         * Creates an image animate
         * it is assumed that all frames of animation are of the same dimensions.*/
        public ImageAnimate(Animation animation){
            this.animation = animation;
        }


        public void layout () {
            if (animation == null) return;

            float width = getWidth();
            float height = getHeight();

            int imageWidth;
            int imageHeight;

            imageWidth = animation.getKeyFrame(0).getRegionWidth();
            imageHeight = animation.getKeyFrame(0).getRegionHeight();

            if ((align & Align.left) != 0)
                imageX = 0;
            else if ((align & Align.right) != 0)
                imageX = (int)(width - imageWidth);
            else
                imageX = (int)(width / 2 - imageWidth / 2);

            if ((align & Align.top) != 0)
                imageY = (int)(height - imageHeight);
            else if ((align & Align.bottom) != 0)
                imageY = 0;
            else
                imageY = (int)(height / 2 - imageHeight / 2);
        }

        public void draw (Batch batch, float parentAlpha) {
            validate();

            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

            float x = getX();
            float y = getY();

            animationTime += Gdx.graphics.getDeltaTime();

            if (animation != null) batch.draw(animation.getKeyFrame(animationTime), x + imageX, y + imageY);

        }

        public void setAlign (int align) {
            this.align = align;
        }
    }
使用的伪代码

Animation animation = new Animation(...);         
ImageAnimate = new ImageAnimate(animation);
ImageAnimate.setBounds(...);

Stage.addActor(ImageAnimate);
这个代码对我来说很好,但是
我的问题是要这样做,并在libgdx中创建,感谢阅读,我希望我解释得很好

扩展图像要容易得多。重写act()以将可绘制的纹理区域设置为关键区域。然后,使用所有正确的几何图形,使用内置的绘图就可以了

你看过这个吗?我不认为有一个内置的方法。我做了类似的事情,但我扩展了Image和basedraw,可以添加一些特性,使其更容易互换。我想我可能更喜欢你的解决方案……简单得多。将
animation+=…
移动到
act
方法可能更有逻辑意义。在某些情况下,您可能希望舞台以不同的速度(如慢动作)表演,因此最好使用舞台的增量时间。@Tenfour04感谢您的回答,我已经过测试,对我来说效果很好,如果您认为它对其他人有用,在libgdx中内置类似的东西,您可以添加一些细节,如果libgdx的创建者喜欢的话,就把它放在libgdx的github中。我想这就是它应该做的,简单地扩展
Actor
(或者在你的例子中是
Widget
)并覆盖
draw()
方法,以便它使用你的
动画。我认为在libgdx中有一个
AnimatedActor
会很酷,但这不是必需的,因为它的实现并不十分复杂。