Home » Android » How to detect the end of action in Libgdx(0.9.7) of android

How to detect the end of action in Libgdx(0.9.7) of android

Posted by: admin June 15, 2020 Leave a comment

Questions:

In the latest version of Libgdx(0.9.7 or libgdx-nightly-20120904),the action listener is removed.So I don’t know how to complete my function efficiently:

  1. move a sprite to the destination.
  2. the movement contains many MoveToActions or MoveByActions.
  3. act the following action after the previous is end.
  4. the rest action may be modified before act.

Actually,the main point is how to act the next action after the previous is end,and I think compare the act time is not a wise method.

Can anybody help me? Thanks !

How to&Answers:

I think that the best approach would be to keep it simple. You could poll if the actor has any action left by using

if (actor.getActions().size() > 0) {no actions left!!} 

A good place to place that code would be near the actor.act();

Anyway, you can execute a secuence of actions using a sequenceAction:

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*; 
...
actor.addAction(sequence(moveTo(200, 100, 2), moveBy(20,30, 3), delay(0.5f), rotateTo(180, 5)));

That would execute those actions one after the other as they finish.

You can check the nightlies documentation for more info here: http://code.google.com/p/libgdx/wiki/scene2d

Is that what you need? (I’m not sure i understood step 4).

If you need something more specific please ask again. You can also take a look at the Actor Class source code to have a better understanding of how actions are handled.
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/Actor.java

Answer:

Not sure if this answers your question, but this is one way to simulate an “actions-completed listener”:

Action completeAction = new Action(){
    public boolean act( float delta ) {
        // Do your stuff
        return true;
    }
};

Action actions = sequence(fadeIn(1f), fadeOut(1f), completeAction);

(Source: http://steigert.blogspot.com.br/2012/07/13-libgdx-tutorial-libgdx-refactoring.html)

Answer:

libgdx 0.9.7

Action a = new Action();
render(){
   if (a.getTime >= a.getDuration())
      Gdx.app.log("a", "is done");
}

Warning in check action isDone:

while

delayAction.getTime > 0 and (delayAction.getTime < delayAction.getDuration)

then

alpha.getTime > alpha.getDuration

until

delayAction.getTime > delayAction.getDuration.

Example:

AlphaAction alpha = new AlphaAction();
alpha.setAlpha(0.2f);
alpha.setDuration(1);

DelayAction delayAction = new DelayAction();
delayAction.setDuration(1);

SequenceAction sequenceAction = new SequenceAction();
sequenceAction.addAction(alpha);
sequenceAction.addAction(delayAction);

More at http://congdongandroid.vn/