【问题标题】:How to use TextureAtlas for animation instead of TextureRegion? - libgdx如何将 TextureAtlas 用于动画而不是 TextureRegion? -libgdx
【发布时间】:2020-11-15 22:34:57
【问题描述】:

我一直在尝试找出最好的方法来用使用 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

精灵表:Image

如果有什么我搞砸了或很容易解决,我会喜欢的。 (我也在使用 Mastering LibGDX Game Development 这本书来指导如何做)。谢谢!

【问题讨论】:

    标签: java libgdx sprite game-engine texture-atlas


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多