11

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

4

2 回答 2

21

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

动画图像.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);
    }
}
于 2014-04-27T13:57:28.663 回答
13

就像你一样,我没有找到动画演员,所以我创建了自己:

动画演员.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);
    }
}
于 2013-04-17T19:47:42.080 回答