【问题标题】:LibGDX is there an actor that is animated?LibGDX 有没有动画的演员?
【发布时间】:2013-04-17 12:08:40
【问题描述】:

在 LibGDX 中是否有一个演员是动画的(接受一个动画)并且当添加到舞台时会自行动画,还是您必须在其中实现自己的 Image 类并自己为其制作动画?

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    我只是创建了一个“AnimatedImage”演员类,它只接受一个动画作为参数(不需要自定义的 Drawable 类)。我认为这个解决方案比上面的解决方案要简单得多。

    AnimatedImage.java:

    public class AnimatedImage extends Image
    {
        protected Animation animation = null;
        private float stateTime = 0;
    
        public AnimatedImage(Animation animation) {
            super(animation.getKeyFrame(0));
            this.animation = animation;
        }
    
        @Override
        public void act(float delta)
        {
            ((TextureRegionDrawable)getDrawable()).setRegion(animation.getKeyFrame(stateTime+=delta, true));
            super.act(delta);
        }
    }
    

    【讨论】:

      【解决方案2】:

      和你一样,我没有找到动画 Actor,所以我自己创建了:

      AnimatedActor.java:

      public class AnimatedActor extends Image
      {
          private final AnimationDrawable drawable;
      
          public AnimatedActor(AnimationDrawable drawable)
          {
              super(drawable);
              this.drawable = drawable;
          }
      
          @Override
          public void act(float delta)
          {
              drawable.act(delta);
              super.act(delta);
          }
      }
      

      AnimationDrawable.java:

      class AnimationDrawable extends BaseDrawable
      {
          public final Animation anim;    
          private float stateTime = 0;
      
          public AnimationDrawable(Animation anim)
          {
              this.anim = anim;
              setMinWidth(anim.getKeyFrameAt(0).getRegionWidth());
              setMinHeight(anim.getKeyFrameAt(0).getRegionHeight());
          }
      
          public void act(float delta)
          {
              stateTime += delta;
          }
      
          public void reset()
          {
              stateTime = 0;
          }
      
          @Override
          public void draw(SpriteBatch batch, float x, float y, float width, float height)
          {
              batch.draw(anim.getKeyFrame(stateTime), x, y, width, height);
          }
      }
      

      【讨论】:

      • 我做了类似的事情。我会等着看是否有人能指出一个实际的 Actor 类,但如果没有,我会将其标记为答案。
      • 非常感谢您的sn-p,好心的先生。
      • 这似乎比“直接”方法慢一点:TextureRegion frame2 = bird_07.getKeyFrame(stateTime, true); stage.getSpriteBatch().begin(); stage.getSpriteBatch().draw(currentFrame, 1000, 700);知道为什么吗?
      • 我认为在这种情况下扩展Image 并不是很有用。您可以扩展Actor 并在其中存储Animation。 Image 希望拥有Drawables,恕我直言,它不像TextureRegions 和其他人那样舒服,您不需要自己制作可绘制的。您还将数据存储两次,分别在ImageAnimatedActor
      • @Mr.Developer 它应该可以工作 Libgdx 理论上是跨平台的,但我没有在 iOS 上测试过。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多