简体   繁体   中英

Libgdx Game: Delay Action Based on Score Count

I'm still a bit new at Java and need some help with a game I'm currently working on. I've already implemented the core of the game where balls drop from the top of the screen and the user controls platforms to bounce the balls to the right side of the screen. If the player succeeds, then a point is awarded. I already implemented the code for the bouncing balls, platforms, score, and various states.

The thing I'm stuck on is controlling the number of balls that drop depending on the score. I already have a rough idea of the algorithm. Without going into too much detail it goes something like this:

public class BallContainer{

public ArrayList<Ball> balls;

public BallContainer(ArrayList<Ball> balls){
  this.balls = balls;
}

public void drop(int howMany){
//code to activate the gravity of "howMany" random balls with a .5 second delay between them

}

public class MainGame{

public void update(float dt){
//check score and drop a selection of balls with random seconds of delay between each group of balls dropped at a time
}
}

I already have an idea of how many balls and how much of a random delay will occur depending on the score. One thing I'm just stuck on the delaying of the action. I know we can use the java.util.Timer and TimerTask, but I also hear libgdx also has some built in delay methods. Anyway, any help would be appreciated.
Thanks.

You can just create a new thread, where you will place a flag, then sleep the newly created thread, and after sleep finishes - set your flag to true.

So ur game won't freeze like it will if you gonna sleep the main thread.

You can use libgx Timer class to provide actions to happen after a certain delay.

Here is an example -

Timer.schedule(new Task() {
    @Override
    public void run() {
        //create new balls here or do whatever you want to do.
    }
}, DELAY_TIME_IN_SECONDS);

What happens here is you are calling a static method of Timer class called schedule which takes a Task and delaySeconds in float as parameters.

Now as parameter you are creating a new Anonymous inner class object as Task is an abstract class. In the anonymous inner class object, you override the run method and put what you want it to do.

Place the above snippet in the place where you want to create new balls or do some action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM