libgdx 中的淡出动作

MAG*_*S94 0 android tween libgdx scene2d

我正在尝试为舞台中的所有演员设置动画,但淡出动作不起作用,尽管其他动作工作正常

我有以下代码

getRoot().setPosition(Gdx.graphics.getWidth(), 0);
getRoot().getColor().a = 0;
SequenceAction sequenceAction = new SequenceAction();
sequenceAction.addAction(Actions.moveTo(0, 0, 3.0f));
sequenceAction.addAction(Actions.fadeOut(3.0f));
getRoot().addAction(sequenceAction);
Run Code Online (Sandbox Code Playgroud)

我还尝试了另一种通过 TweenEngine 为它们设置动画的方法,class GroupAccessor implements TweenAccessor<Group>除了操作 alpha 值外,所有工作都很好

public class GroupAccessor implements TweenAccessor<Group> {

    public static final int ALPHA = 0;
    public static final int POS_XY = 1;

    @Override
    public int getValues(Group target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case ALPHA :
                returnValues[0] = target.getColor().a;
                return 1;
            case POS_XY :
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                return 2;
            default:
                assert false;
                return -1;
        }
    }

    @Override
    public void setValues(Group target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case ALPHA :
                target.setColor(target.getColor().r, target.getColor().g, target.getColor().b,
                        newValues[0]);
                break;
            case POS_XY :
                target.setPosition(newValues[0], newValues[1]);
                break;
            default:
                assert false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 TweenEngine 制作动画

Timeline.createSequence().
                push(Tween.set(getRoot(), GroupAccessor.POS_XY).target(Gdx.graphics.getWidth(),0))
                .push(Tween.to(getRoot(), GroupAccessor.POS_XY, 3.0f).target(0,0))
                .push(Tween.set(getRoot(), GroupAccessor.ALPHA).target(0))
                .push(Tween.to(getRoot(), GroupAccessor.ALPHA, 3.0f).target(1)).start(manager);
Run Code Online (Sandbox Code Playgroud)

我有多个演员,我制作了其中一些演员的数组。这是 2 个演员的 draw() 方法的 2 个示例

public void draw(Batch batch, float parentAlpha) {
        batch.draw(wallSprite, 0, 0, Constants.WIDTH, Constants.HEIGHT);
    }

public void draw(Batch batch, float parentAlpha) {
        batch.draw(gearSprite, getX(), getY(),
                gearSprite.getOriginX(), gearSprite.getOriginY(), gearSprite.getWidth(),
                gearSprite.getHeight(), gearSprite.getScaleX(), gearSprite.getScaleY()
                , gearSprite.getRotation());
        try {
            batch.draw(permittedCSprite, getX() + gearSprite.getWidth()/2
                            - permittedCSprite.getWidth()/2, getY() +
                            gearSprite.getHeight()/2 - permittedCSprite.getHeight()/2,
                    permittedCSprite.getWidth(), permittedCSprite.getHeight());
        }
        catch (NullPointerException e) {}
    } 
Run Code Online (Sandbox Code Playgroud)

Ten*_*r04 5

drawActor的方法绝对必须在 Batch 上设置颜色。这是支持衰落所必需的。即使您不关心褪色或更改演员的颜色,您仍然需要将 Batch 的颜色更改为白色以确保绘制演员的颜色(因为无法保证其他颜色演员已离开批次。

通常,您会在 draw 方法的顶部有这样的内容:

public void draw (Batch batch, float parentAlpha) {
    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    //...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您的 Actor 绘制的是 Sprite 而不是 TextureRegion,则您必须在 Sprite 上设置颜色。这是因为精灵有自己的颜色,当您调用sprite.draw.

如果您使用的是 Sprite(我不推荐与 Actors 一起使用),您的绘制方法应如下所示:

public void draw (Batch batch, float parentAlpha) {
    sprite.setColor(getColor());
    sprite.draw(batch, parentAlpha);
}
Run Code Online (Sandbox Code Playgroud)

一个“问题”是 Sprite 是 TextureRegion 的子类。Sprite 是一个 TextureRegion,它保存有关颜色、大小、方向、位置等的信息,因此它可以比 TextureRegion 更快(可能)绘制。它以使用更多内存为代价。如果您使用的是精灵,则应该调用sprite.draw not batch.draw(sprite ...),因为第二个选项将精灵视为 TextureRegion。

如果要使用 Sprite,则需要将所需的所有属性应用到 sprite。例如:

public void draw (Batch batch, float parentAlpha) {
    sprite.setColor(getColor());
    sprite.setPosition(getX(), getY());
    sprite.setSize(getWidth(), getHeight());
    //etc. etc. etc. :(
    sprite.draw(batch, parentAlpha);
}
Run Code Online (Sandbox Code Playgroud)

出于这个原因,在 Actor 中使用 Sprite 真的没有任何意义。太多的冗余。只需使用TextureRegions。