【问题标题】:Sprite animation Libgdx JavaSprite 动画 Libgdx Java
【发布时间】:2014-07-13 16:23:20
【问题描述】:

试图有一个旋转和动画的二维、自上而下的精灵,我有一个渲染它的主类,以及一个单独的“资产”类来创建它。旋转有效,但精灵保持在同一帧上。我使用 libgdx wiki 尝试为我的 sprite 设置动画 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);

我需要做什么才能正确地为精灵设置动画?

【问题讨论】:

    标签: java animation libgdx sprite


    【解决方案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);
     }
    

    【讨论】:

      【解决方案2】:
      // 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-16
        • 1970-01-01
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多