Using LibGDX, how do you have animations using separate images?

I am learning how to make games using LibGDX and I am trying to make a small platform (using Eclipse). I made 4 images on the main character to animate when he moves. However, I cannot find anything online to show me how to make an animation without using SpriteSheet. How do I animate using 4 different images?

+1


source to share


2 answers


First of all: you shouldn't use different images. It may not really matter to your player (because there is only one), but in general you should always use sprite sheets, aka TextureAtlas

.

However, this is possible without it, using different textures.



TextureRegion tex1 = new TextureRegion(new Texture("play_anim_1"));
TextureRegion tex2 = new TextureRegion(new Texture("play_anim_2"));
TextureRegion tex3 = new TextureRegion(new Texture("play_anim_3"));
TextureRegion tex4 = new TextureRegion(new Texture("play_anim_4"));

Animation playerAnimation = new Animation(0.1f, tex1, tex2, tex3, tex4);

      

+5


source


You have to use TexturePacker with TextureAtlas. Adding each texture manually is not the right way.

The texture packer packs your multiple 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'll post code examples in a few hours when I get home.

I actually had a separate class for handling asset loading:

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 the animation:

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

      

My original animations were running__001.png, run_002.png, ...

Since your filenames are in the format name_0001.png, the texture pack puts animation keyframes in one file, and they all have the same name "name" and an additional parameter "index", which is a number in your file name, for example 001, 002, 003 etc.

And it Assets.atlas.findRegions("run")

returns an array with keyframes.

+1


source







All Articles