libgdx不工作
问题描述:
My animation is not working 2D动画,仅显示静态图片。我根据libgdx/wiki的例子做了一切。为什么它不起作用?libgdx不工作
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
以便传递true如getKeyFrame (float stateTime, boolean looping)
方法的第二个参数。
@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
变更数据的stateTime
到float
从int
类型。
是您Thorns.png图像2个图像并排?你是否在等待1.025秒以查看帧改变?它应该是非循环的,所以它只显示1.025秒的第一个图像? – dfour
我的理解动画根本不会启动。一切都在第一帧。 环流式更改为true,没有任何改变。 – GRbI3yH
我将帧更改时间增加了10.0f,但没有像以前那样发生 – GRbI3yH