2d动画libgdx无法正常工作

时间:2017-08-03 13:06:45

标签: java animation libgdx 2d

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

    }
}

动画根本无法启动

1 个答案:

答案 0 :(得分:0)

相当可能的动画正在运行但你没有得到他的观点,因为你有两帧动画,帧持续时间为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的数据类型从float更改为int