我試圖加載一個爆炸動畫。動畫由16幀組成,全部保存在文件Explosion.png中。在我的遊戲中,所有圖像都存儲在紋理圖集包中。使用TextureAtlas的LibGdx動畫
首先,我得到了,這將創造爆炸,我需要從類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);
}
我因爲我有16幀,所以使用4x4。和渲染方法中我得到了以下內容:
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);
}
動畫工作,但是圖像從整地圖集文件加載,而不是隻Explosion.png在步驟1中指定
您需要使用動畫並將其添加到其中每個幀AFAIK – 2017-08-03 14:11:08
region.getTexture()獲取地圖集的整個紋理。您應該使用region.findRegion(「spritesheetname」)來獲取名稱的區域。 – dfour
@dfour我已經在一個子類(爆炸= atlas.findRegion(Constants.EXPLOSION);),然後獲取結果。 –