【问题标题】:Multiple animations into one object Animation多个动画合成一个对象动画
【发布时间】:2015-09-25 17:20:08
【问题描述】:

我现在正在尝试在播放器类中使用我的动画表,但我对我的情况有些怀疑。我正在关注文档 (https://github.com/libgdx/libgdx/wiki/2D-Animation),其中讨论了 2d 动画,但它只显示了一种类型的动画,在这种情况下,是步行动画。我的上下文不同,我有 3 种类型的动画:空闲、跑步和攀爬。我想知道是否可以将所有类型的动画保存在一个唯一的 Animation 对象中,或者我必须为每个对象创建 3 个对象?我也尝试在 stackoverflow 和 google 上找到相同的东西,但我什么也没找到。

这就是我对私有方法所做的loadAnimations()

private void loadAnimations() {
    // Get the player animations sheet
    Texture playerSheet = Assets.manager.get(Assets.playersheet);

    // List of texture regions that hold the animations
    TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Put all textures from texture region to 1-d texture region
    int index = 0;
    TextureRegion[] walkFrames = new TextureRegion[PLAYER_FRAME_COLS * PLAYER_FRAME_ROWS];
    for (int i = 0; i < PLAYER_FRAME_ROWS; i++) {
        for (int j = 0; j < PLAYER_FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }
    this.runAnimation = new Animation(FRAMES_DURTION, walkFrames);
}

这是 Player proprietes:

private Animation idleAnimation, runAnimation, climbAnimation;
private TextureRegion currentFrame;

在这种情况下该怎么办?谢谢。

【问题讨论】:

    标签: animation libgdx


    【解决方案1】:

    为了利用 Animation 类中的方法,我可能会创建 3 个单独的 Animation 对象,每个“状态”(空闲、运行、爬升)一个。您拥有的代码(以及教程中的代码)会为您的一种状态加载动画纹理。因此,您需要对所有 3 个动画纹理执行类似的操作,以创建 3 个不同的动画对象。

    然后在您的渲染方法中,根据您的角色所处的“状态”,您选择要渲染的动画。

    例如,在下面的行

    currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    

    您可以根据角色的动作来选择要渲染的动画(idleAnimation、runAnimation、climbAnimation)。 (在动画之间切换时,您可能还需要使用 stateTime 值),但希望您能理解我的建议。

    @Override
        public void render() {
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        
            stateTime += Gdx.graphics.getDeltaTime();           
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);   
            spriteBatch.begin();
            spriteBatch.draw(currentFrame, 50, 50);             
            spriteBatch.end();
        }
    

    【讨论】:

      【解决方案2】:

      我解决了我的问题。基本上我是这样做的:

      这是我的动画变量:

      // Player Animations
      public static int PLAYER_FRAME_ROWS  =   3;
      public static int PLAYER_FRAME_COLS  =   4;
      public static float FRAMES_DURTION   = 0.13f;
      public static enum ANIMATIONS_INDEX {IDLE, RUNNING, CLIMBING};
      private Animation animations[];
      private TextureRegion currentFrame;
      private float stateTime, currentFrameDuration;
      private int animationIndex;
      

      在这里,在 loadAnimations() 函数上,我这样做了:

      private void loadAnimations() {
          // Instance the number of types of animations
          this.animations = new Animation[ANIMATIONS_INDEX.values().length];
      
          // Get the player animations sheet
          Texture playerSheet = Assets.manager.get(Assets.playersheet);
      
          // List of texture regions that hold the animations
          TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);
      
          // Set the textures regions for each animation index
          for (int i = 0; i < ANIMATIONS_INDEX.values().length; i++) {
              this.animations[i] = new Animation(FRAMES_DURTION, tmp[i]);
          }
      
          // Set player bounds
          this.setBounds(this.getX(), this.getY(), playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);
      
          // Set state time to zero
          this.stateTime = 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多