无法循环操作。 libGDX

时间:2016-01-07 01:00:11

标签: 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();


}

但它仍然有效。它会动作一次并停止。当我需要循环时。从蓝色到金色,然后从金色到蓝色。 我真的很感激任何帮助,因为我只是在学习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);
}

这是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();
}

现在它没有改变颜色,它总是蓝色。

3 个答案:

答案 0 :(得分:3)

您遇到了与期望与Actors一起使用的操作相关的错误。很多Actions在没有Actor的情况下工作正常,但是SequenceAction没有,因为它希望附加到Actor以检测它是否应该仍在运行。

所以一个有点hacky的解决方案是在你设置它时为你的顶级动作添加一个虚拟actor。这模拟了当您向舞台中的演员添加动作时会发生什么。

repeatAction.setActor(new Actor());

如果您计划在应用中使用更多操作,则可以创建一个用于管理所有操作的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());

当然,如果您想使用舞台绘制内容,您只需将动作添加到舞台上,而不是将Actor用作动作管理器。

我个人觉得scene2d动作必须比UniversalTweenEngine更容易使用和设置。

答案 1 :(得分:1)

以下示例适用于我(无限旋转)

方法1 :(推荐)

loadingActor.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(360, 1)));

方法2:

Image loadingActor = new Image(AssetsController.getInstance().getLoading());
loadingActor.setOrigin(Align.center);
final SequenceAction infiniteRotate = Actions.sequence();
infiniteRotate.addAction(Actions.rotateTo(0 , 0f) );
infiniteRotate.addAction(Actions.rotateTo(360 , 1f) );
loadingActor.addAction(Actions.forever(infiniteRotate));

答案 2 :(得分:0)

Libgdx行动仅适用于Libgdx演员。您正在创建一个操作但不将其分配给Actor。您只需将shapeRenderer颜色设置为蓝色,而忽略您创建的操作。如果你想随时间改变颜色,我建议你使用Universal tween engine的补间。

或者,您可以放弃shapeRenderer并使用Image(Actor的子类)。然后,您将操作分配给图像。但要做到这一点,你必须创建一个舞台并将你的影像添加到舞台上。请参阅scene2d wiki

无论哪种方式都是一些额外的步骤。也许一个廉价的黑客可以轻松实现这个:

  1. 只需创建一个图片:

    图片i =新图片();

  2. 将您的操作分配给图像

    i.addAction(repeatAction);

  3. 替换repeatAction.act(...)

    i.act(Gdx.graphics.getDeltaTime());

  4. 将shapeRenderer.setColor(蓝色)更改为

    shapeRenderer.setColor(i.getColor());

  5. 希望有所帮助。