【问题标题】:Animation in libgdx using individual Textureslibgdx 中使用单个纹理的动画
【发布时间】:2014-03-14 09:00:07
【问题描述】:

如何使用单独的精灵或纹理在 libgdx 中实现动画。我尝试通过将图像打包到 TexturePacker 中并制作一个 png 文件和指定的映射文件 here but 它无法完美运行,因为只有 3 张图像可以制作动画。告诉我如何使用单个图像或纹理制作动画。

【问题讨论】:

    标签: android animation textures libgdx


    【解决方案1】:

    首先,如果您有 3 张图片并且您想从中创建一种精灵,它们确实需要相同的尺寸。我会把它作为一种演员来实现。如果您不喜欢演员的想法,只需删除扩展并为其添加位置和大小。还将act 更改为update 并删除draw(...,...) 的覆盖。

    public class IndividualSprite extends Actor {
    
        private Array<Texture> textures; //meight change it to TextureRegions which also contains a texture but sizes and positions
        private float intervaltime = 0;
        private float interval;
        private int count = 0;
    
        /**
         * Varargs<br>
         * @param timeIntervall
         * @param t
         *            Varargs. Parse all textures here as list or simply with ,t1
         *            ,t2 , t3
         */
        public IndividualSprite(float timeIntervall, Texture... t) {
            this.interval = timeIntervall;
            this.textures = new Array<Texture>();
            for (Texture texture : t) {
                this.textures.add(texture);
            }
            // now sett default width height and pos
            this.setX(0);
            this.setY(0);
            this.setWidth(textures.get(0).getWidth());
            this.setWidth(textures.get(0).getHeight());
        }
    
        public void addTexture(Texture... t) {
            for (Texture texture : t) {
                this.textures.add(texture);
            }
        }
    
        public void deleteTexture(Texture... t) {
            for (Texture texture : t) {
                this.textures.removeValue(texture, true);
            }
        }
    
        public void setInterval(float i) {
            this.interval = i;
        }
    
        @Override
        public void act(float delta) {
            super.act(delta);
            this.intervaltime += delta;
            if (this.intervaltime >= interval) {
                if (count == textures.size) {
                    count = 0;
                } else {
                    count++;
                }
                this.intervaltime = 0;
            }
        }
    
        @Override
        public void draw(SpriteBatch batch, float alpha) {
            batch.begin();
            batch.draw(textures.get(count), getX(), getY(), getWidth(), getHeight());
            batch.end();
        }
    }
    

    但是你可以给一个演员动作来移动或其他什么。它是一种自定义的Sprite。这只是一种方法。应该有更多,甚至更好。您还可以扩展 Sprite 类并更改周围的东西,以便它需要多个纹理等。试试看。如果您更改我提到的内容,这是一个演员解决方案或非演员解决方案。不要忘记调用.act/update,否则它只会显示第一个FirstTexture

    【讨论】:

    • 感谢您的关注。我用另一个简单的代码做到了。
    【解决方案2】:

    找到了如下所述的答案:

        region1 = new TextureRegion(texture1);
        region2 = new TextureRegion(texture2);
        region3 = new TextureRegion(texture3);
    
        animation = new Animation(0.08f, region1, region2, region3);         
    

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多