Using LibGDX, how do you have an animation using s

2019-01-20 05:45发布

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 animation when he moves. However, I can't find anything online to show me how to make an animation without using a SpriteSheet. How do you make an animation using 4 different images ?

2条回答
Viruses.
2楼-- · 2019-01-20 06:36

First of all: You should not use different images. Maybe for your player it does not matter much (because there is only one) but in general you should always use sprite sheets, a.k.a. TextureAtlas.

However, it is possible without it by 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);
查看更多
Summer. ? 凉城
3楼-- · 2019-01-20 06:46

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.

查看更多
登录 后发表回答