【问题标题】:LibGDX ColorAction.restart() doesnt workLibGDX ColorAction.restart() 不起作用
【发布时间】:2016-04-07 13:55:56
【问题描述】:

我正在尝试循环播放 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


    【解决方案1】:

    首先,对setColor 的调用告诉它要修改哪个Color 实例,因此您不想向它传递像Color.BLUE 这样的伪常量,否则您将修改“常量”本身。 LibGDX 颜色类是可变的,所以即使BLUEstatic final,它仍然可以修改。您需要创建自己的 Color 实例以用于操作并将其传入。

    myColor = new Color(Color.BLUE);
    

    要进行颜色循环,您需要两个 ColorAction(您切换的两种颜色各有一个),并且它们需要一起在一个 SequenceAction 中。它们都应该设置为相同的 Color 实例。然后,如果您希望它循环多次,请将 SequenceAction 包装在 RepeatAction 中。

    【讨论】:

    • 感谢您的回答。我只是做了以下步骤: 创建了 2 种颜色:Color blue = new Color(Color.BLUE); Color gold = new Color(Color.GOLD); 然后我设置了 ColorAction 的颜色选项:actionBtG.setColor(blue); actionGtB.setColor(gold); 创建了 SequenceAction:sequenceAction = new SequenceAction(actionBtG,actionGtB); 并创建了带有 FOREVER 标志的 RepeatAction:repeatAction.setAction(sequenceAction); 但它仍然不起作用适当地。我想我还是做错了什么。我对 libGDX 和 java 也有点陌生,但我真的很想了解这件事。
    • 它们都需要指向同一个颜色实例。不要创造蓝色和金色。只需创建两种操作都会修改的一种颜色。在形状渲染器上使用该颜色对象。
    • 啊,我以为我明白了,但还是不行。在第一篇文章中添加了一些信息。
    【解决方案2】:

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

    这对您来说意味着:尝试再次设置颜色,这样您将重新开始您的操作!

    编辑: 我现在用我自己的类替换了 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);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多