2017-08-03 76 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的帮助 –