Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot loop an action. libGDX

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


}

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

Here is 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.

like image 284
Gtzzy Avatar asked Jan 07 '16 01:01

Gtzzy


2 Answers

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

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

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.

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

like image 145
Tenfour04 Avatar answered Sep 20 '22 23:09

Tenfour04


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));
like image 23
ultra.deep Avatar answered Sep 20 '22 23:09

ultra.deep