2017-08-03 47 views
0

我試圖加載一個爆炸動畫。動畫由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中指定

+0

您需要使用動畫並將其添加到其中每個幀AFAIK – 2017-08-03 14:11:08

+0

region.getTexture()獲取地圖集的整個紋理。您應該使用region.findRegion(「spritesheetname」)來獲取名稱的區域。 – dfour

+0

@dfour我已經在一個子類(爆炸= atlas.findRegion(Constants.EXPLOSION);),然後獲取結果。 –

回答

1

代碼您Particles類中:

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

替換:

​​

而且

提請您使用textureRegion代替SpriteBatch繪製textureRegion他texture因此,選擇合適的方法簽名Animation

+1

完美的謝謝你,我正在尋找如何拆分TextureRegion。也感謝@dfour的幫助 –