无法循环操作。 libGDX
Cannot loop an action. 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();
}
现在颜色不变,一直是蓝色。
Libgdx 动作仅适用于 Libgdx 演员。您正在创建一个动作,但没有将其分配给 Actor。您只是将 shapeRenderer 颜色设置为蓝色,而不考虑您创建的操作。如果您想随时间更改颜色,我建议使用 Universal tween engine 中的补间。
或者,您可以放弃 shapeRenderer 并使用图像(Actor 的子类)。然后将您的操作分配给图像。但要做到这一点,您必须创建一个舞台并将您的图像添加到舞台上。见 scene2d wiki.
任何一种方法都需要一些额外的步骤。也许一个便宜的 hack 可以让这个工作更容易:
只需创建一个图像:
图片 i = 新图片();
将你的动作分配给图像 i
i.addAction(repeatAction);
用
替换repeatAction.act(...)
i.act(Gdx.graphics.getDeltaTime());
将shapeRenderer.setColor(蓝色)更改为
shapeRenderer.setColor(i.getColor());
希望对您有所帮助。
您遇到了一个与预期与 Actors 一起使用的动作相关的错误。许多动作在没有 Actor 的情况下也能正常工作,但 SequenceAction 却不行,因为它希望附加到 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 Actions 必须比 UniversalTweenEngine 更易于使用和设置。
所以下面的示例对我有用(无限旋转)
方法一:(推荐)
loadingActor.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(360, 1)));
方法二:
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));
我在 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();
}
现在颜色不变,一直是蓝色。
Libgdx 动作仅适用于 Libgdx 演员。您正在创建一个动作,但没有将其分配给 Actor。您只是将 shapeRenderer 颜色设置为蓝色,而不考虑您创建的操作。如果您想随时间更改颜色,我建议使用 Universal tween engine 中的补间。
或者,您可以放弃 shapeRenderer 并使用图像(Actor 的子类)。然后将您的操作分配给图像。但要做到这一点,您必须创建一个舞台并将您的图像添加到舞台上。见 scene2d wiki.
任何一种方法都需要一些额外的步骤。也许一个便宜的 hack 可以让这个工作更容易:
只需创建一个图像:
图片 i = 新图片();
将你的动作分配给图像 i
i.addAction(repeatAction);
用
替换repeatAction.act(...)i.act(Gdx.graphics.getDeltaTime());
将shapeRenderer.setColor(蓝色)更改为
shapeRenderer.setColor(i.getColor());
希望对您有所帮助。
您遇到了一个与预期与 Actors 一起使用的动作相关的错误。许多动作在没有 Actor 的情况下也能正常工作,但 SequenceAction 却不行,因为它希望附加到 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 Actions 必须比 UniversalTweenEngine 更易于使用和设置。
所以下面的示例对我有用(无限旋转)
方法一:(推荐)
loadingActor.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(360, 1)));
方法二:
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));