动画添加到舞台

时间:2015-10-20 07:47:24

标签: java android animation libgdx box2d

我创建了,正如您在下面看到的动画,但现在,我无法弄清楚如何添加到舞台。谁能告诉我怎么样?我在网上搜索并且不清楚它的想法。谢谢

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

3 个答案:

答案 0 :(得分:3)

就像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);

答案 1 :(得分:1)

最好的想法是创建一个扩展Actor类的actor,它将包装Animation对象。然后在他的 act mathod中,您获得了当前的KeyFrame,并且在 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());
        }
    }

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

这种方法更好,因为:

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

答案 2 :(得分:0)

溶液

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