编程语言
首页 > 编程语言> > java – 无法循环操作. libGDX

java – 无法循环操作. libGDX

作者:互联网

我问过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();
}

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

解决方法:

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

所以一个有点hacky的解决方案是在你设置它时为你的顶级动作添加一个虚拟actor.这模拟了当您向舞台中的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更容易使用和设置.

标签:java,android,libgdx,scene2d
来源: https://codeday.me/bug/20190523/1156733.html