gpt4 book ai didi

android - libgdx 中使用单独纹理的动画

转载 作者:行者123 更新时间:2023-11-30 03:01:53 25 4
gpt4 key购买 nike

如何使用单个 Sprite 或纹理在 libgdx 中实现动画。我尝试通过在 TexturePacker 中打包图像并制作一个 png 文件和指定的映射文件来做到这一点 here 但它不能完美工作,因为只有 3 个图像可以制作动画。告诉我如何使用单个图像或纹理制作动画。

最佳答案

好的,首先,如果您有 3 张图片,并且您想用它创建一种 Sprite ,那么它们确实需要相同的尺寸。我会把它作为一种 Actor 来实现。如果您不喜欢 Actor 的想法,只需删除扩展并为其添加位置和大小。同时将 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();
}
}

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

关于android - libgdx 中使用单独纹理的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com