图像动画在舞台LibGDX内

时间:2014-12-16 19:38:54

标签: libgdx

我需要一张照片,动画显示在stage内,因为我不知道是否有任何方法可以建立,但是,我想知道这是一个好方法还是更有效率并在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中创建,感谢阅读,我希望我能解释清楚

1 个答案:

答案 0 :(得分:0)

更容易扩展图像。重写act()以将drawable的纹理区域设置为键区域。然后,只需使用所有正确几何体的内置绘图即可。