使用LibGDX,你如何使用单独的图像动画?
我正在学习如何使用LibGDX制作游戏,我正在尝试制作一个小型平台游戏(使用Eclipse)。 我在主角上制作了4张图像,以便在移动时制作动画。 但是,我在网上找不到任何东西,告诉我如何在不使用SpriteSheet的情况下制作动画。 你如何使用4种不同的图像制作动画?
首先:你不应该使用不同的图像。 也许对你的播放器来说它并不重要(因为只有一个),但一般来说你应该总是使用精灵表,也就是TextureAtlas
。
但是,没有它可以使用不同的纹理。
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);
您应该使用TextureAtlas的TexturePacker。 手动添加每个纹理不是正确的方法。
纹理打包器将您的多个图像打包成一个图像。 使用如下名称:img_01.png,img_02.png等,然后在Array中的一行代码中提取它们。
我回家后几个小时就会发布代码示例。
我实际上有一个单独的类来处理资产加载:
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(); } }
加载动画的代码:
playerRun = new Animation(1/10f, Assets.atlas.findRegions("run")); playerRun.setPlayMode(Animation.PlayMode.LOOP);
我原来的动画图片是run_001.png,run_002.png,…
因为您的文件名格式为name_0001.png,所以纹理包装器将动画关键帧放在一个文件中,它们都有一个名称“name”和一个附加参数“index”,即文件名中的数字,例如001,002, 003等
而Assets.atlas.findRegions("run")
返回一个带有关键帧的数组。