简体   繁体   English

Sprite动画Libgdx Java

[英]Sprite animation Libgdx Java

Trying to have a 2-d, top down sprite that rotates and is animated, i have the main class where it is rendered, and a separate 'assets' class where it is created. 试图让一个二维的,自上而下的精灵旋转并动画化,我在渲染它的主类中创建了一个单独的“资产”类。 The rotation works, however the sprite stays on the same frame. 旋转有效,但是子画面停留在同一帧上。 I used the libgdx wiki to try and animate my sprite https://github.com/libgdx/libgdx/wiki/2D-Animation 我使用libgdx Wiki尝试对我的精灵进行动画处理https://github.com/libgdx/libgdx/wiki/2D-Animation

Here is the code in the assets class: 这是资产类中的代码:

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);
    }

And here is how it is rendered: 这是它的呈现方式:

game.batch.draw(Asset.currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270);

What do i need to do to correctly animate the sprite? 我需要怎么做才能正确设置精灵的动画?

The frame will never animate because you are not updating it. 框架永远不会动画,因为您没有更新它。 In the load() method you did the following: load()方法中,您执行了以下操作:

stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);  

but this will get executed only once when you call the load() method. 但这仅在调用load()方法时执行一次。

As your Asset class implements the Screen interface you must have implemented the abstract method render(float delta) , so you need to update the frame in that method as follows: 当您的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);
  }
}

walk frames 8x8 步行架8x8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM