无法循环操作.libGDX

War*_*rdS 5 java android libgdx scene2d

我问过4天前的问题.得到了一些帮助,现在我的代码看起来像

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 = new RepeatAction():        
    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();


}
Run Code Online (Sandbox Code Playgroud)

但它仍然有效.它会动作一次并停止.当我需要循环时.从蓝色到金色,然后从金色到蓝色.我真的很感激任何帮助,因为我只是在学习libGDX.谢谢.

我已经阅读了所有答案并编辑了我的代码,但它仍然无效:

private Actor actionManager = new Actor();
ColorAction actionBtG = new ColorAction();
ColorAction actionGtB = new ColorAction();
SequenceAction sequenceAction;
RepeatAction repeatAction;
Color activeColor = new Color(Color.BLUE);
ShapeRenderer shapeRenderer;


@Override
public void create () {

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

    actionGtB.setColor(activeColor);
    actionGtB.setEndColor(Color.BLUE);
    actionGtB.setDuration(5);
    sequenceAction = new SequenceAction(actionBtG,actionGtB);
    repeatAction = new RepeatAction();
    repeatAction.setAction(sequenceAction);
    repeatAction.setCount(RepeatAction.FOREVER);

    actionManager.addAction(repeatAction);
}
Run Code Online (Sandbox Code Playgroud)

这是render()

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

    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(blue);
    shapeRenderer.rect(100, 100, 40, 40);
    shapeRenderer.end();
}
Run Code Online (Sandbox Code Playgroud)

现在它不会改变颜色,它总是蓝色的.

Ten*_*r04 3

您遇到了与期望与 Actor 一起使用的操作相关的错误。许多 Action 在没有 Actor 的情况下也能正常工作,但 SequenceAction 却不能,因为它希望附加到 Actor 来检测它是否仍应运行。

因此,一个有点hacky的解决方案是在设置时将虚拟演员添加到您的顶级操作中。这模拟了当您向舞台中的演员添加动作时发生的情况。

repeatAction.setActor(new Actor());
Run Code Online (Sandbox Code Playgroud)

如果您计划在应用程序中使用更多操作,则可以创建一个用于管理所有操作的 Actor:

private Actor actionManager = new Actor();

//when creating actions:
actionManager.addAction(repeatAction);

//In render(), use this instead of calling act on individual actions
//It will call act on all actions in the actor:
actionManager.act(Gdx.graphics.getDeltaTime());
Run Code Online (Sandbox Code Playgroud)

当然,如果您想使用舞台来绘制东西,您可以简单地将动作添加到舞台,而不是使用 Actor 作为动作管理器。

我个人认为 scene2d Actions 一定比 UniversalTweenEngine 更容易使用和设置。