0

I am using libgdx and i have several game objects with different methods i want the methods in my timer to happen one after another but in libgdx they happen all at once i dont know how to fix it

    timer.scheduleTask( task = new Task(){ public void run(){
    rando =rn.nextInt(8);
    Gdx.app.log("the num is", Integer.toString(rando));
    if(rando == 0){
        bush.rustle();
        bush2.rustle2();
        bush3.dontrustle3();
        bush4.dontrustle4();
        enemy.shoot();
        enemy2.shoot();
        enemy3.godown();
        enemy4.godown();


    }
     if(rando == 1){
        bush.dontrustle();
        bush2.rustle2();
        bush3.rustle3();
        bush4.dontrustle4();
        enemy.godown();
        enemy2.shoot();
        enemy3.shoot();
        enemy4.godown();

    }
    if(rando == 2){
        bush.rustle();
        bush2.dontrustle2();
        bush3.rustle3();
        bush4.rustle4();
        enemy2.godown();
        enemy.shoot();
        enemy4.shoot();
        enemy3.shoot();


    }

    if(rando == 3){
        bush.rustle();
        bush2.rustle2();
        bush3.rustle3();
        bush4.rustle4();
        enemy.shoot();
        enemy2.goup();
        enemy4.goup();
        enemy3.shoot();

    }

    if(rando == 4){
        bush.rustle();
        bush2.rustle2();
        bush3.rustle3();
        bush4.rustle4();
        enemy2.godown();
        enemy.godown();
        enemy4.shoot();
        enemy3.shoot();


    }

how it works is that everytime a number is called a set of methods run but they run all at once i want them to run one after the other

ChukaApps
  • 21
  • 3
  • 2
    The method themself *do* run one after another. Your problem probably is that the *effects* the methods creates happen all at once. I suggest using some event queue to store upcoming tasks to do, once a task is complete, next from the queue can start. – kajacx Oct 03 '15 at 17:21

1 Answers1

0

You might look at the libgdx-ai extension. It has some utilities for building up and reacting to events. The state machine is probably overkill for what you want, but might have ideas you can crib from.

See also https://gamedev.stackexchange.com/questions/14568/integrating-an-ai-state-machine-with-actions-that-take-more-than-1-tick

A lighter-weight approach might be something like this: http://vanillajava.blogspot.com/2011/06/java-secret-using-enum-as-state-machine.html (though I recommend doing this mostly as a way to understand the more complex AI library state machine).

Community
  • 1
  • 1
P.T.
  • 24,557
  • 7
  • 64
  • 95