LibGDX ColorAction.restart()不起作用

问题描述 投票:1回答:2

我正在尝试循环ColorAction动画。这是我的代码:

ColorAction action = new ColorAction();
ShapeRenderer shapeRenderer;

@Override
public void create () {

    shapeRenderer = new ShapeRenderer();
    action.setColor(Color.BLUE);
    action.setEndColor(Color.GOLD);
    action.setDuration(5);



}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    int fps = Gdx.graphics.getFramesPerSecond();
    curFrame++;
    if (curFrame == fps*5) {
        action.restart();
        curFrame = 0;
    }

    action.act(Gdx.graphics.getDeltaTime());
    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(action.getColor());
    shapeRenderer.rect(100, 100, 40, 40);
    shapeRenderer.end();



}

但是这不能正常工作。它只播放一次动画然后停止。有人可以解释一下我在做什么错吗?谢谢。

经过几处更改:

ColorAction actionBtG = new ColorAction();
ColorAction actionGtB = new ColorAction();
SequenceAction sequenceAction;
RepeatAction repeatAction = new RepeatAction();
ShapeRenderer shapeRenderer;
Color blue = new Color(Color.BLUE);

@Override
public void create () {

    shapeRenderer = new ShapeRenderer();
    actionBtG.setColor(blue);
    actionBtG.setEndColor(Color.GOLD);
    actionBtG.setDuration(5);

    actionGtB.setColor(blue);
    actionGtB.setEndColor(Color.BLUE);
    actionGtB.setDuration(5);
    sequenceAction = new sequenceAction(actionBtG,actionGtB);

    repeatAction.setAction(sequenceAction);
    repeatAction.setCount(RepeatAction.FOREVER);
}

@Override
public void render () {

    repeatAction.act(Gdx.graphics.getDeltaTime());
    Gdx.gl.glClearColor(1,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(blue);
    shapeRenderer.rect(100, 100, 40, 40);
    shapeRenderer.end();


}
java android libgdx scene2d
2个回答
1
投票

问题解决了吗? ...几分钟前我遇到了同样的问题...问题是,如果操作结束,则startcolor(在您的情况下为蓝色)与endcolor具有相同的颜色。所以你看不到任何动作!我通过创建一个新实例解决了这个问题,但是我认为,重置开始颜色也应该可行。我稍后再试。

这对您而言意味着:请尝试重新设置颜色,否则您将重新开始操作!

编辑:我现在用自己的班级替换了ColorAction:

public class ColorTransitionAnimation extends TemporalAction {
private final Color start = new Color();
private final Color end = new Color();
private final Color animatedColor = new Color();
private RepeatType repeatType = RepeatType.REVERSE;

public enum RepeatType { REVERSE, RESTART }

public void setRepeatType(RepeatType repeatType) {
    this.repeatType = repeatType;
}

protected void update (float percent) {
    float r = start.r + (end.r - start.r) * percent;
    float g = start.g + (end.g - start.g) * percent;
    float b = start.b + (end.b - start.b) * percent;
    float a = start.a + (end.a - start.a) * percent;
    animatedColor.set(r, g, b, a);
}

@Override
protected void end() {
    super.end();
    if(repeatType == RepeatType.REVERSE)
        setReverse(!isReverse());

    restart();
}

public Color getAnimationColor () {
    return animatedColor;
}

public void setStartColor (Color color) {
    start.set(color);
}

public void setEndColor (Color color) {
    end.set(color);
}

}


1
投票

首先,对setColor的调用会告诉它要修改的Color实例,因此,您不想将其传递给Color.BLUE之类的伪常量,否则您将要修改“ constant”本身。 LibGDX颜色类别是可变的,因此即使BLUEstatic final,仍可以对其进行修改。您需要创建自己的Color实例以与该操作配合使用,然后将其传入。

myColor = new Color(Color.BLUE);

要进行颜色循环,您需要两个ColorAction(在两种颜色之间进行切换时,每个都要一个),并且它们必须一起位于SequenceAction中。两者都应设置为相同的Color实例。然后,如果您希望它循环多次,则将SequenceAction包装在RepeatAction中。

© www.soinside.com 2019 - 2024. All rights reserved.