Unable to execute loop. libGDX

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

But it still does not work properly. He does the action once and stops. When I need to go in cycles. From blue to gold, then from gold to blue. I would really appreciate any help because I am just learning libGDX. Thanks.

I read all the answers and edited my code, but it still doesn't 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); } 

Here is the 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 does not change color, it is always blue.

+5
source share
3 answers

You encounter an error related to the actions that you intend to use with the Actors. Many actions work fine without an Actor, but SequenceAction is not because it expects it to be tied to an Actor to determine if it should still work.

Thus, a somewhat hacky solution would be to add a fictitious actor to your top-level action when you set it up. This mimics what happens when you add an action to an actor in a stage.

 repeatAction.setActor(new Actor()); 

If you plan to use more actions in your application, you can create one actor, which you use to control 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()); 

And, of course, if you want to use Stage to draw material, you can simply add your actions to the scene instead of using the Actor as your action manager.

I personally believe that scene2d Actions should be easier to use and configure than UniversalTweenEngine.

+3
source

Libgdx Actions are for Libgdx Actors only. You create an action but do not assign it to an actor. You simply set the shapeRenderer color to blue except for the action you created. I would recommend using animations from the Universal tween engine if you want to change color over time.

Alternatively, you can cut out the shapeRenderer and use Image (a subclass of Actor). Then you assigned your action to the image. But for this you need to create a scene and add your image to the scene. See scene2d wiki .

In any case, this will be a few additional steps. Perhaps a cheap hack to this job is easy:

  • just create an image:

    Image i = new image ();

  • assign your action to image i

    i.addAction (repeatAction);

  • replace repeatAction.act (...) with

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

  • change shapeRenderer.setColor (blue) to

    shapeRenderer.setColor (i.getColor ());

Hope this helps.

0
source

So, the following sample worked for me (infinite rotation)

Method 1: (recommended)

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

Method 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)); 
0
source

Source: https://habr.com/ru/post/1240038/


All Articles