Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 985
Qiengo Avatar asked Sep 04 '12 09:09

Qiengo


3 Answers

use Actions.run :

Action action = Actions.sequence(
        Actions.delay(1f),
        Actions.run(new Runnable() {
            @Override public void run() {
                //  end of action
            }
        })
);
like image 77
ultra.deep Avatar answered Oct 20 '22 11:10

ultra.deep


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)

like image 43
Roar Skullestad Avatar answered Oct 21 '22 07:10

Roar Skullestad


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

like image 9
aacotroneo Avatar answered Oct 21 '22 06:10

aacotroneo