8

i have a game(like super jumper, this game is a jumping game) that our character has life. after collision with enemies, his life reduce. and i want to after 1 sec , calculate the collisions. i mean in this 1 sec, if my character contact with enemies , nothing happen and he continue his way. for this , i define a boolean variable in my GameScreen class, name "collision" and another in Wolrd class, name "collBirds". after one contact with enemy collision and collBirds change to true. but i want after 1 sec collistion change to false. i use several things like System.currentTimeMillis() and "for loop",and nothing happen. i'm not so good in 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;
        }
    }
Hosein
  • 475
  • 2
  • 7
  • 21
  • 1
    Check out [this question](http://gamedev.stackexchange.com/questions/25001/waiting-specific-time-to-increase-sound-libgdx) – Piotr Praszmo May 29 '12 at 11:48
  • tnx for your reply. i read it and use it. but i change something in my game, and finally it worked. – Hosein May 30 '12 at 09:01

3 Answers3

38

In some cases you could also want to use com.badlogic.gdx.utils.Timer

Example usage:

float delay = 1; // seconds

Timer.schedule(new Task(){
    @Override
    public void run() {
        // Do your work
    }
}, delay);
Glogo
  • 2,694
  • 2
  • 23
  • 23
7

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

Then each loop, you will need to add the time since last check to the variable, and check if it's more than a second.

timeSinceCollision += deltaTime;
if(timeSinceCollision > 1.0f) {
    // do collision stuff
} else {
    // ignore the collision
}
Matsemann
  • 21,083
  • 19
  • 56
  • 89
0

If you want to do this in same thread than you can use Thread.sleep(). But in this case your current thread will freeze and if this is single thread game, than your whole game will freeze. If you don't want your game to freeze for 1 second, than you should spawn the thread and in that thread call the sleep and after sleep, change the flag

ejb_guy
  • 1,125
  • 6
  • 6