My animation is not working, only a static picture is displayed. I did everything according to the example from libgdx/wiki. Why does not it work?
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);
}
}
Animation does not start at all
Quite possible animation is running but you're not getting his view, because you've two frame animation with frame duration 1.025 sec
. And your Animation is running without loop.
Try looped Aniamation
so pass true as in second parameter of getKeyFrame (float stateTime, boolean looping)
method.
@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);
}
EDIT
Change data type of stateTime
to float
from int
.