尝试使用旋转且动画的2-d自上而下的精灵,我有渲染它的主要类,以及单独的资产'创建它的类。旋转有效,但精灵停留在同一帧上。我使用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);
我需要做些什么来正确设置精灵的动画?
答案 0 :(得分:1)
框架永远不会动画,因为您没有更新它。在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);
}
答案 1 :(得分:0)
// 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);
}
}