I'm recreating the classic arcade game Space Invaders in java. My problem arises when dealing with sprites after killing an enemy. Since the death sprite is one of the sprites an enemy can have, what I'm basically doing is changing the sprite shown, then removing the enemy object. Is there a way I can have the sprite be shown for around a second, then have the enemy object be removed? I'm also open to other ways to do something like this since it might not be as efficient as some other option I haven't thought of.
Edit: I should’ve mentioned this before, but anything involving sleeping the thread causes the entire game to be delayed, which won’t work.
// stuff detecting collision between missile and enemy
missiles.remove(0);
enemies.get(j).loadImage(2); // loads death sprite for enemy that got hit
// one second delay
enemies.remove(j);
Edit: I figured out the solution to my problem. Using the swing Timer class I was able to create a delay after the death sprite is shown then have the enemy be removed. Posting this in case someone with a similar problem needs it.
Timer timer = new Timer(600, new ActionListener() {
public void actionPerformed(ActionEvent e) {
enemies.remove(remove); // The enemy is removed when after the allotted delay occurs when timer.start() is called
}
});
// final code after changes
missiles.remove(0);
enemies.get(j).loadImage(2);
remove = j;
timer.setRepeats(false); // makes it so that only one enemy is removed
timer.start(); // after the delay the enemy is removed, enough time for the death sprite to be shown for the allotted delay