Search code examples
javalibgdxspritegame-enginetexture-atlas

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


I've been trying to figure out the best way to replace this code with something that uses TextureAtlas instead of splitting the texture into different areas. Would using that be easier?

 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);
}

Here is the atlas and the image

Atlas:

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

Sprite Sheet: Image

If there's something I messed up on or a very easy fix, I would love that. (I'm also using the Mastering LibGDX Game Development book for guidence on what to do). Thank you!


Solution

  • In the original texture image files, put an underscore before the frame numbers. For example

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

    Then you can get the whole list from the atlas as an Array to pass to your Animation constructor:

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