Wednesday, 28 August 2013

What is a good way to pause a thread?

What is a good way to pause a thread?

I'm creating a java 2D platformer game, and I'm having a little trouble
getting an animation to go through when the player dies. When the player
dies, all enemies are removed and an explosion animation is played where
they used to be. At the same time, the player begins to blink. I want that
to go on for about two seconds, and then have my setState() method switch
to the "PlayerDeadState", which is basically the retry or return to main
menu option screen. I've used Thread.sleep(), but it doesn't work, and
I've heard it's bad for GUI threads. Here is my code: public void update()
{ // check if player is dead if(player.dead == true) {
player.flinching = true;
for(int i = 0; i < enemies.size(); i++) {
Enemy e = enemies.get(i);
e.update();
e.hit(200);
if(e.isDead()) {
enemies.remove(i);
i--;
eExplosions.add(
new Explosion(e.getx(), e.gety()));
}
}
gsm.setState(3);
}
}
The animations go through if I comment out my setState() method.
Any suggestions?

No comments:

Post a Comment