二维动画 libgdx 不工作
2d animation libgdx is not working
My animation is not working, only a static picture is displayed. I did everything according to the example from 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.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
。
My animation is not working, only a static picture is displayed. I did everything according to the example from 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.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
。