简体   繁体   English

Libgdx游戏:基于得分计数的延迟动作

[英]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. 我在Java领域还很新,需要一些有关当前正在开发的游戏的帮助。 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. 我知道我们可以使用java.util.Timer和TimerTask,但我也听说libgdx也有一些内置的延迟方法。 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. 您可以只创建一个新线程,在其中放置一个标志,然后休眠新创建的线程,并在休眠完成后-将您的标志设置为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. 您可以使用libgx Timer类提供一定延迟后发生的操作。

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. 这里发生的是您正在调用Timer类的静态方法,该方法称为schedule,该方法将Taskfloat delaySeconds作为参数。

Now as parameter you are creating a new Anonymous inner class object as Task is an abstract class. 现在,作为参数,您正在创建一个新的Anonymous内部类对象,因为Task是一个抽象类。 In the anonymous inner class object, you override the run method and put what you want it to do. 在匿名内部类对象中,您将覆盖run方法并放入您希望它执行的操作。

Place the above snippet in the place where you want to create new balls or do some action. 将以上代码片段放在您要创建新球或执行某些操作的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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