【问题标题】:2d animation libgdx is not working二维动画 libgdx 不工作
【发布时间】:2017-08-03 13:06:44
【问题描述】:

My animation is not working,只显示静态图片。我按照libgdx/wiki 的示例做了所有事情。为什么它不起作用?

public class Thorns  extends GameObject implements IGameObject  {

    Animation<TextureRegion> animation;
    private static final int FRAME_COLS = 1, FRAME_ROWS = 2;
    public Thorns(Texture texture, Body body) {
        super(texture, body,false);
        Texture walkSheet = new Texture("Thorns.png");
        TextureRegion[][] tmp = TextureRegion.split(walkSheet,
            walkSheet.getWidth() / FRAME_COLS,
            walkSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] walkFrames = new TextureRegion[FRAME_ROWS*FRAME_COLS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        animation = new Animation<TextureRegion>(1.025f, walkFrames);
    }

    @Override
    public void dispose() {

    }

    int stateTime;

    @Override
    public void draw(SpriteBatch spriteBatch) {
        stateTime += Gdx.graphics.getDeltaTime();

        TextureRegion currentFrame = (TextureRegion) animation.getKeyFrame(stateTime, false);

        spriteBatch.draw(currentFrame, body.getTransform().getPosition().x, body.getTransform().getPosition().y);
        //drawSprite(spriteBatch);

    }
}

动画根本没有开始

【问题讨论】:

  • 您的 Thorns.png 图像是 2 张并排的图像吗?您是否等待 1.025 秒才能看到帧变化?它应该是非循环的,所以它只显示第一个图像 1.025 秒?
  • 据我了解,动画根本没有开始。一切都保持在第一帧。 Looping 更改为 true,没有任何变化。
  • 我将换帧时间增加了 10.0f,但没有像以前那样发生任何事情

标签: java animation libgdx 2d


【解决方案1】:

很可能动画正在运行,但您没有看到他的视图,因为您有两帧动画,帧持续时间为1.025 sec。您的动画正在无循环运行。

尝试循环 Aniamation 以便在 getKeyFrame (float stateTime, boolean looping) 方法的第二个参数中传递 true。

@Override
public void draw(SpriteBatch spriteBatch) {
    stateTime += Gdx.graphics.getDeltaTime();
    TextureRegion currentFrame =  animation.getKeyFrame(stateTime, true);
    spriteBatch.draw(currentFrame, body.getTransform().getPosition().x, body.getTransform().getPosition().y);
}

编辑

stateTime 的数据类型从int 更改为float

【讨论】:

  • 动画根本没有开始。一切都保持在第一帧。循环更改为 true,没有任何变化
  • @GRbI3yH 将stateTime的数据类型更改为float
猜你喜欢
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2018-10-29
相关资源
最近更新 更多