使用单个纹理在libgdx中进行动画

时间:2014-03-14 09:00:07

标签: android animation textures libgdx

如何使用单个精灵或纹理在 libgdx 中实现动画。我试图将这些图像打包在TexturePacker中,并按照指定here 创建一个png文件和映射文件但它没有完美运作,因为只有3个动画图像。告诉我如何使用单个图像或纹理进行动画制作。

2 个答案:

答案 0 :(得分: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类并更改周围的内容,以便它需要多个纹理等等。试一试吧。如果你改变我提到的东西,这是一个Actor解决方案或非actor的解决方案。别忘了给.act/update打电话,否则它只会显示第一个FirstTexture

答案 1 :(得分:0)

找到如下所述的答案:

    region1 = new TextureRegion(texture1);
    region2 = new TextureRegion(texture2);
    region3 = new TextureRegion(texture3);

    animation = new Animation(0.08f, region1, region2, region3);