gpt4 book ai didi

java - 使用TextureAtlas的LibGdx动画

转载 作者:行者123 更新时间:2023-12-02 02:40:55 26 4
gpt4 key购买 nike

我正在尝试加载爆炸动画。动画由 16 帧组成,全部保存在 Explosion.png 文件中。在我的游戏中,所有图像都存储在纹理图集包中。

所以首先我从 Assets.java 类中得到了我需要的区域

public class Explosion {
public final AtlasRegion explosion;
public Explosion (TextureAtlas atlas){
explosion = atlas.findRegion(Constants.EXPLOSION);
}
}

然后在我将创建爆炸的类中,我有以下代码:

public Particles(Vector2 position){
this.position = position;
startTime = TimeUtils.nanoTime();

Array<TextureRegion> explosionAnimationTexture = new Array<TextureRegion>();

TextureRegion region = Assets.instance.explosion.explosion;
Texture explosionTexture = region.getTexture();

int ROW = 4; // rows of sprite sheet image
int COLUMN = 4;
TextureRegion[][] tmp = TextureRegion.split(explosionTexture, explosionTexture.getWidth() / COLUMN, explosionTexture.getHeight() / ROW);
TextureRegion[] frames = new TextureRegion[ROW * COLUMN];
int elementIndex = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
explosionAnimationTexture.add(tmp[i][j]);
frames[elementIndex++] = tmp[i][j];
}
}

explosion = new Animation(EXPLOSION_FRAME_DURATION,explosionAnimationTexture , Animation.PlayMode.LOOP_PINGPONG);
}

我使用 4x4,因为我有 16 帧。在渲染方法中我得到以下内容:

public void render(SpriteBatch batch){
float elapsedTime = MathUtils.nanoToSec * (TimeUtils.nanoTime() - startTime);
TextureRegion walkLoopTexture = explosion.getKeyFrame(elapsedTime);

batch.draw(
walkLoopTexture.getTexture(),
position.x,
position.y,
0,
0,
walkLoopTexture.getRegionWidth(),
walkLoopTexture.getRegionHeight(),
0.3f,
0.3f,
0,
walkLoopTexture.getRegionX(),
walkLoopTexture.getRegionY(),
walkLoopTexture.getRegionWidth(),
walkLoopTexture.getRegionHeight(),
false,
false);

}

动画正在运行,但是图像是从整个图集文件加载的,而不仅仅是步骤 1 中指定的 Explosion.png。

最佳答案

Particles 类中的代码:

TextureRegion region = Assets.instance.explosion.explosion;
TextureRegion[][] tmp = TextureRegion.split(explosionTexture, explosionTexture.getWidth() / COLUMN, explosionTexture.getHeight() / ROW);

替换为:

TextureRegion[][] tmp = region.split(region.getRegionWidth()/COLUMN,region.getRegionHeight()/ROW);
<小时/>

使用textureRegion而不是他的texture绘制您的Animation,因此选择SpriteBatch的适当方法签名来绘制textureRegion .

关于java - 使用TextureAtlas的LibGdx动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486522/

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