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

对着背影说爱祢 提交于 2019-11-28 11:51:02

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!