尝试拥有一个可旋转和动画的二维自上而下的 Sprite ,我有渲染它的主类,以及创建它的单独的“ Assets ”类。旋转有效,但 Sprite 保持在同一帧上。我使用 libgdx wiki 尝试为我的 Sprite 设置动画 https://github.com/libgdx/libgdx/wiki/2D-Animation
这是 Assets 类中的代码:
public class Asset implements ApplicationListener, Screen{
public static Texture walkSheet;
private static final int FRAME_COLS = 4;
private static final int FRAME_ROWS = 2;
static Animation walkAnimation;
static TextureRegion[] walkFrames;
static TextureRegion currentFrame;
static SpriteBatch spriteBatch;
static float stateTime;
public static void load(){
walkSheet = new Texture(Gdx.files.internal(".png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS); // #10
walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(0.1f, walkFrames); // #11
spriteBatch = new SpriteBatch(); // #12
stateTime = 0f;
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
}
这是它的渲染方式:
game.batch.draw(Asset.currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);
我需要做什么才能正确地为 Sprite 设置动画?
最佳答案
框架永远不会动画,因为您没有更新它。在 load()
方法中,您执行了以下操作:
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
但是当您调用 load()
方法时,这只会执行一次。
由于您的 Asset
类实现了 Screen
接口(interface),因此您必须实现抽象方法 render(float delta)
,因此您需要更新该方法中的框架如下:
public void render(float delta)
{
stateTime += delta;
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
// Then render the frame as follows
batch.draw(currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);
}
关于java - Sprite 动画 Libgdx Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724778/