且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Libgdx 动画不起作用

更新时间:2023-01-25 18:08:47

当您使用 TexturePacker 时,有一种更简单的方法来创建动画.

When you use TexturePacker there is an easier way to create animations.

当我们想要创建跑步动画时
首先,您将所有动画图像添加到 TexturePacker,您必须查看每一帧都有相同的名称 + 下划线 + frameNumber.所以帧的名称必须是:run_1、run_2、run_3 等.

When we want to create a run animation
First you ad all animation images to TexturePacker you have to look that every frame has the same name + underscore + frameNumber. So the name of the frames must be: run_1, run_2, run_3 etc.

当我们创建它时,我们的资产中有一个 run.txt 和 run.png.

When we created it we have a run.txt and run.png in our assets.

现在我们可以使用 AssetManager 加载 TextureAtlas:

Now we can load the TextureAtlas with our AssetManager:

AssetManager assetManager = new AssetManager();
assetManager.load("run.txt", TextureAtlas.class);
assetManager.finishLoading();
TextureAtlas runAnimationAtlas = assetManager.get("run.txt", TextureAtlas.class);

有了这个 TextureAtlas,我们可以创建我们的动画:

With this TextureAtlas we can create our Animation:

float frameDuration = 0.1f; // the duration between the animation frames
Animation<TextureRegion> runAnimation = new Animation<TextureRegion>(frameDuration, runAnimationAtlas.findRegions("run"), Animation.PlayMode.LOOP);

并渲染动画:

batch.draw(runAnimation.getKeyFrame(stateTimer),posX, posY)