如何将 TextureAtlas 用于动画而不是 TextureRegion? - libgdx

How to use TextureAtlas for animation instead of TextureRegion? - libgdx

我一直在努力找出用使用 TextureAtlas 而不是将纹理拆分为不同区域的东西替换此代码的最佳方法。使用它会更容易吗?

 private void loadAllAnimations(){
    // Walking animation
    Texture texture = Utility.getTextureAsset(characterSpritePath); // Gets default.png
    TextureAtlas atlas = Utility.getAtlas(characterAtlasPath); Gets default.atlas
    TextureRegion[][] textureFrames = TextureRegion.split(texture, FRAME_WIDTH, FRAME_HEIGHT);
    walkDownFrames = new Array<TextureRegion>(3);
    walkLeftFrames = new Array<TextureRegion>(3);
    walkRightFrames = new Array<TextureRegion>(3);
    walkUpFrames = new Array<TextureRegion>(3);

    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 3; j++){
            TextureRegion region = textureFrames[i][j];
            if (region == null){
                Gdx.app.debug(TAG, "Got null animation frame " + i + "," + j);
            }
            switch (i){
                case 0:
                    walkDownFrames.insert(j, region);
                    break;
                case 1:
                    walkLeftFrames.insert(j, region);
                    break;
                case 2:
                    walkRightFrames.insert(j, region);
                    break;
                case 3:
                    walkUpFrames.insert(j, region);
                    break;
            }
        }
    }

    walkDownAnimation = new Animation<>(0.25f, walkDownFrames, Animation.PlayMode.LOOP);
    walkLeftAnimation = new Animation<>(0.25f, walkDownFrames, Animation.PlayMode.LOOP);
    walkRightAnimation = new Animation<>(0.25f, walkRightFrames, Animation.PlayMode.LOOP);
    walkUpAnimation = new Animation<>(0.25f, walkUpFrames, Animation.PlayMode.LOOP);
}

这是图集和图片

图集:

DefaultDood.png
size: 512,64
format: RGBA8888
filter: Nearest,Nearest
repeat: none
DefaltManBack
  rotate: false
  xy: 342, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManBackWalkin1
  rotate: false
  xy: 138, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManBackWalkin2
  rotate: false
  xy: 36, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManForwardWalkin1
  rotate: false
  xy: 2, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManForwardWalkin2
  rotate: false
  xy: 206, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManFront
  rotate: false
  xy: 376, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManLeftStill
  rotate: false
  xy: 240, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManLeftWalkin1
  rotate: false
  xy: 70, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManLeftWalkin2
  rotate: false
  xy: 274, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManRightWalkin1
  rotate: false
  xy: 308, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManRightWalkin2
  rotate: false
  xy: 172, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1
DefaltManSideRightStill
  rotate: false
  xy: 104, 2
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: -1

雪碧Sheet:Image

如果有什么地方我搞砸了或者有一个非常简单的修复方法,我会很乐意的。 (我还使用 Mastering LibGDX Game Development 一书来指导如何操作)。谢谢!

在原始纹理图像文件中,在帧数之前加上下划线。例如

  • DefaltManBackWalkin_1.png
  • DefaltManBackWalkin_2.png
  • DefaltManBackWalkin_3.png
  • DefaltManBackWalkin_4.png
  • 等等

然后你可以从图集中获取整个列表作为数组传递给你的动画构造函数:

Animation<TextureRegion> defaultManBackWalkinAnimation =
    Animation<>(0.25f, textureAtlas.findRegions("DefaltManBackWalkin"));