无法循环操作. libGDX [英] Cannot loop an action. libGDX
问题描述
我已经在4天前问过一个问题.得到了一些帮助,现在我的代码看起来像
I've asked a question like 4 days ago. Got some help and now my code looks like
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.谢谢.
But it still works wrong. It does action once and stops. When I need to loop that. From blue to gold, then from gold to blue. I would really appreciate any help, because I'm just learning libGDX. Thanks.
我已经阅读了所有答案并编辑了我的代码,但仍然无法正常工作:
I have read all of the answers and edited my code, but it still doesnt work:
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();
}
现在它没有改变颜色,它总是蓝色.
Now it's not changing the color, it's always blue.
推荐答案
您遇到了一个与预期与Actor一起使用的Action相关的错误.很多Action在没有Actor的情况下也可以正常工作,但是SequenceAction却不能,因为它希望附加到Actor上以检测它是否应该仍在运行.
You're encountering a bug related to Actions expecting to be used with Actors. A lot of Actions work fine without an Actor, but SequenceAction does not because it expects to be attached to an Actor to detect if it should still be running.
因此,一种有点棘手的解决方案是在设置虚拟角色时将其添加到您的顶级操作中.这可以模拟当您在舞台中向演员添加动作时发生的情况.
So a somewhat hacky solution would be to add a dummy actor to your top level action when you set it up. This simulates what happens when you add an action to an actor in a stage.
repeatAction.setActor(new Actor());
如果您打算在应用中使用更多操作,则可以创建一个用于管理所有操作的Actor:
If you plan to use more Actions in your app, you can create a single Actor that you use to manage all your actions:
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作为动作管理器.
And of course, if you want to use a Stage for drawing stuff, you could simply add your actions to the Stage instead of using an Actor as your action manager.
我个人发现Scene2d Action必须比UniversalTweenEngine更易于使用和设置.
I personally find scene2d Actions must easier to use and set up than UniversalTweenEngine.
这篇关于无法循环操作. libGDX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!