无法循环操作.库GDX

时间:2023-04-06
本文介绍了无法循环操作.库GDX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 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);
}

这里是渲染()

@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 在没有 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());

当然,如果您想使用 Stage 来绘制东西,您可以简单地将您的动作添加到 Stage,而不是使用 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 动作必须比 UniversalTweenEngine 更易于使用和设置.

I personally find scene2d Actions must easier to use and set up than UniversalTweenEngine.

这篇关于无法循环操作.库GDX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 Android 设备上通过 libGDX Preferences 正确保存游戏进度? 下一篇:我的 Admob 广告没有绘图,但有吗?(LibGDX Admob 6.4.1)

相关文章