[英]Sprite animation Libgdx Java
試圖讓一個二維的,自上而下的精靈旋轉並動畫化,我在渲染它的主類中創建了一個單獨的“資產”類。 旋轉有效,但是子畫面停留在同一幀上。 我使用libgdx Wiki嘗試對我的精靈進行動畫處理https://github.com/libgdx/libgdx/wiki/2D-Animation
這是資產類中的代碼:
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);
我需要怎么做才能正確設置精靈的動畫?
框架永遠不會動畫,因為您沒有更新它。 在load()
方法中,您執行了以下操作:
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
但這僅在調用load()
方法時執行一次。
當您的Asset
類實現Screen
接口時,您必須已經實現了抽象方法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);
}
// pass the array of movements to AnimatedActor
// animationFrames = walkSheetArray[moveDirection];
// animation = new Animation(1f / 5f, animationFrames);
// myAnimatedActor = new AnimatedActor(animation, rotation)
public class AnimatedActor extends Image {
private float stateTime = 0;
Animation animation;
public AnimatedActor(Animation animation, float rotation) {
super(animation.getKeyFrame(0));
this.animation = animation;
this.rotation = rotation;
}
@Override
public void act(float delta) {
((TextureRegionDrawable) getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true));
myAnimatedActor.setRotation(rotation);
super.act(delta);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.