Using LibGDX, how do you have an animation using seperate images ?

后端 未结 2 528
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 21:48

I\'m learning how to make games using LibGDX and I\'m trying to make a small platform game (using Eclipse). I made 4 images on the main character running to make an animatio

2条回答
  •  無奈伤痛
    2020-12-10 22:33

    You should use a TexturePacker with TextureAtlas. Adding every texture by hand is not the right way.

    The texture packer packs your several images into one image. Use names like this: img_01.png, img_02.png etc, and you extract them all in one line of code in an Array.

    I will post code examples in a few hours when i get home.

    I actually had an separate class for dealing with loading of assets:

    public abstract class Assets {
    private static AssetManager asm = new AssetManager();
    private static String assetsPackPath = "assets.pack";
    public static TextureAtlas atlas;
    
    public static void load(){
        asm.load(assetsPackPath, TextureAtlas.class);
        asm.finishLoading();
    
        atlas = asm.get(assetsPackPath);
    }
    
    public static void dispose(){
        asm.dispose();
    }
    }
    

    And the code that loads animation :

    playerRun = new Animation(1/10f, Assets.atlas.findRegions("run"));
    playerRun.setPlayMode(Animation.PlayMode.LOOP);
    

    My original animation images were run_001.png, run_002.png, ...

    Because your file names have a format name_0001.png the Texture packer puts animation keyframes in one file and they all have one name "name" and a additional parameter "index" that is the number in your file name, for example 001, 002, 003, etc.

    And Assets.atlas.findRegions("run") returns an Array with the keyframes.

提交回复
热议问题