为什么用TexturePacker绘制的动画会绘制黑色正方形?

时间:2019-07-30 07:53:06

标签: java libgdx

我正在尝试将TexturePacker与LibGDX一起使用,以播放器动画对带进行动画处理。但是,它绘制的是黑匣子,而不是实际的精灵。我的代码有问题吗?

这是我当前的地图集文件:

size: 320, 32
format: RGBA8888
filter: Linear,Linear
repeat: none
idle
  rotate: false
  xy: 0, 0
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: 1
idle
  rotate: false
  xy: 32, 0
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: 2
jump
  rotate: false
  xy: 64, 0
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: 1
jump
  rotate: false
  xy: 96, 0
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: 2
run
  rotate: false
  xy: 128, 0
  size: 32, 32
  orig: 32, 32
  offset: 0, 0
  index: 1

这是动画控制器出现问题的地方:

public class Animator {

    TextureAtlas atlas;
    private Animation<TextureRegion> idleAnimation;
    private Animation<TextureRegion> runningAnimation;
    private Animation<TextureRegion> jumpingAnimation;

    private TextureRegion currentFrame;

    // Variable for tracking elapsed time for animation
    private float elapsedTime;

    public Animator(){

        atlas = new TextureAtlas(Gdx.files.internal("ninja.atlas"));
        TextureAtlas.AtlasRegion idle = atlas.findRegion("idle");
        TextureAtlas.AtlasRegion running = atlas.findRegion("run");
        TextureAtlas.AtlasRegion jumping = atlas.findRegion("jump");
        atlas.dispose();

        idleAnimation = new Animation<TextureRegion>(1/6f, idle);
        runningAnimation = new Animation<TextureRegion>(1/12f, running);
        jumpingAnimation = new Animation<TextureRegion>(1/12f, jumping);

        elapsedTime = 0f;
    }

    public void render(SpriteBatch batch){
        elapsedTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time

        System.out.println(currentFrame);
        batch.begin();
        batch.draw(idleAnimation.getKeyFrame(elapsedTime, true), 100, 50); // draw current frame at (50, 50)
        batch.end();
    }

}

我没有收到任何错误消息,只是绘制了黑框而不是精灵。

2 个答案:

答案 0 :(得分:0)

地图集中的框架应这样命名:

  • idle_0
  • idle_1
  • run_0
  • run_1
  • jump_0

如果您用下划线命名“区域”,然后命名为“帧号”,则“动画”只能在Atlas中找到动画的TextureRegions。

您可以在LibGdx Wiki中找到描述:https://github.com/libgdx/libgdx/wiki/2D-Animation

  

TexturePacker和TextureAtlas提供了一种方便的方式来生成动画。动画的所有源图像都应在结尾处带有下划线和帧号,例如running_0.png,running_1.png,running_2.png等。TexturePacker会自动将这些数字用作帧号(只要包装参数useIndexes为true)。


此外,如果您希望动画多次运行,则应将动画的播放模式设置为LOOP。默认播放模式为NORMAL,因此在完成所有帧后,您始终将看到渲染的最后一个帧,而不是从第一个帧开始。

您可以在构造函数中设置PlayMode:

idleAnimation = new Animation<TextureRegion>(1/6f, idle, Animation.PlayMode.LOOP);

或使用setPlayMode方法:

idleAnimation.setPlayMode(Animation.PlayMode.LOOP);

答案 1 :(得分:0)

许多Android设备不支持非2的幂次方纹理,并且当您尝试加载1时,它将加载为黑色,而没有错误消息。我看到您的纹理的宽度为320,不是2的幂。我建议使用TexturePacker创建地图集。它具有用于使纹理使用二维幂的设置,默认设置为on。