【问题标题】:Animation add to the Stage动画添加到舞台
【发布时间】:2015-10-20 07:47:24
【问题描述】:

我创建了一个动画,如下所示,但现在,我不知道如何添加到舞台。谁能告诉我怎么做?我在网上搜索并不清楚关于它的想法。谢谢

TextureRegion tex1 = new TextureRegion(new Texture("play_anim_1"));
TextureRegion tex2 = new TextureRegion(new Texture("play_anim_2"));
TextureRegion tex3 = new TextureRegion(new Texture("play_anim_3"));
TextureRegion tex4 = new TextureRegion(new Texture("play_anim_4"));

Animation playerAnimation = new Animation(0.1f, tex1, tex2, tex3, tex4);

你可以做类似的事情

stage.addAnimation ( playerAnimation ) ; 

?

【问题讨论】:

    标签: java android animation libgdx box2d


    【解决方案1】:

    就像m.antkowicz的代码一样,创建类:

    import com.badlogic.gdx.graphics.g2d.Animation;
    import com.badlogic.gdx.graphics.g2d.Batch;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.scenes.scene2d.Actor;
    
    public class AnimeActor extends Actor{
    
    
        Animation animation;
        TextureRegion currentRegion;
    
        float time = 0f;
    
        public AnimeActor(Animation animation) {
            this.animation = animation;
        }
    
        @Override
        public void act(float delta){
            super.act(delta);
            time += delta;
    
            currentRegion = animation.getKeyFrame(time, true);
        }
    
        @Override
        public void draw(Batch batch, float parentAlpha) {
            super.draw(batch, parentAlpha);
            batch.draw(currentRegion, getX(), getY());
        }
    }
    

    使用:

    AnimeActor anim = new AnimeActor(animation);
    stage.addActor(anim);
    

    【讨论】:

      【解决方案2】:

      最好的办法是创建一个扩展Actor 类的actor,它将包装动画对象。然后在他的 act 方法中,您将获得当前的关键帧,在 draw 方法中,您可以根据演员的位置渲染它

          class MyAnimation extends Actor
          {
              Animation animation;
              TextureRegion currentRegion;
      
              float time = 0f;
      
              //... creating animation etc...
      
              @Override
              public void act(float delta){
                  time += delta;
      
                  currentFrame = animation.getKeyFrame(time, true);
              }
      
              @Override
              public void draw(Batch batch, float parentAlpha) {
                  super.draw(batch, parentAlpha);
                  batch.draw(currentRegion, getX(), getY());
              }
          }
      

      现在您可以创建演员并将其添加到舞台。


      这种方法更好,因为:

      • 您不需要在渲染屏幕的方法中处理渲染
      • Z-index 将始终保持 - 在您的示例中,动画将始终覆盖所有内容,因为它是在 Stage 之后呈现的
      • 您可以在单个类中包装更多代码,甚至可以继承它来创建下一个动画类型或将动画与身体等...

      【讨论】:

      • 我可以试试你的想法,你连动画都写完整?
      【解决方案3】:

      解决方案

      public void render () {
          //qui definisco lo stage
          Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
          stage.act(Gdx.graphics.getDeltaTime());
          stage.draw();
      
          elapsedTime += Gdx.graphics.getDeltaTime();
      
          batch.begin();
          batch.draw(seaAnimation.getKeyFrame(elapsedTime,true),100,100);
          batch.end();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-22
        • 2013-03-10
        • 2013-04-28
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 2019-06-25
        • 1970-01-01
        • 2014-08-29
        相关资源
        最近更新 更多