Set the delay in the game libgdx

I have a game (for example, a super-jumper, this game is a jumping game) that our character has life. after a collision with enemies, his life decreases. and I want to calculate collisions after 1 second. I mean for this 1 second, if my character contacts the enemies, nothing happens, and he continues on his way. for this I define a boolean in the GameScreen class, the name is "collision" and another in the Wolrd class, the name is "collBirds". after one contact with an enemy collision, and collBirds change to true. but I want it to change to false after a one second collision. I use a few things like System.currentTimeMillis () and "for loop" and nothing happens. I am not so good at java.

this is my condition:

if(World.collBirds == true && collition == false){ life -= 1; lifeString = "Life : " + life; World.collBirds = false; collition = true; for (??? "need to stay here for 1 sec" ???) { collition = false; } } 
+7
source share
3 answers

In some cases, you can also use com.badlogic.gdx.utils.Timer

Usage example:

 float delay = 1; // seconds Timer.schedule(new Task(){ @Override public void run() { // Do your work } }, delay); 
+32
source

When the first collision occurs, set a float timeSinceCollision = 0;

Then in each cycle, you will need to add the time since the last check to the variable and check if it was more than a second.

 timeSinceCollision += deltaTime; if(timeSinceCollision > 1.0f) { // do collision stuff } else { // ignore the collision } 
+6
source

If you want to do this in the same thread, you can use Thread.sleep (). But in this case, your current thread will freeze, and if it is a single-threaded game, then your whole game will freeze. If you do not want your game to freeze for 1 second, than you should create a thread and cause sleep in this thread even after sleep, change the flag

0
source

All Articles