-1

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
Taha
  • 1
  • 2
  • I’ve tried that already but it doesn’t work well since it stops the entire game for a second. – Taha Jun 25 '20 at 08:07
  • What gui framework are you using? You usually can't just add a sleep in the Event Dispatch Thread. – k5_ Jun 25 '20 at 08:09
  • Do you have a main game loop? You could just remember to remove the enemy in _N_ loop iterations instead of blocking – lupz Jun 25 '20 at 08:11
  • If you are using Swing, then SwingWorker is there to handle the UI thread separately. It will not hang your UI thread. – Vikas Dubey Jun 25 '20 at 08:25

2 Answers2

-1

Thread.sleep(1000); should do it for you.

user11809641
  • 815
  • 1
  • 11
  • 22
  • 2
    While this is the appropriate answer for the title, it doesn't really work for UI applications, as suggested by OPs text. – Joachim Sauer Jun 25 '20 at 08:08
  • I see. Wasn't specified when I wrote this... – user11809641 Jun 25 '20 at 08:11
  • Please don't flag incorrect answers as low quality. [See this Meta](https://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) – Machavity Jun 25 '20 at 22:42
-1

Multiple options are there:

sleep() method from Thread class(catch InterruptedException):

Thread.sleep(1000);

sleep() method from TimeUnit enum of java.util.concurrent package:

TimeUnit.SECONDS.sleep(1);
TimeUnit.MINUTES.sleep(1);
Vikas Dubey
  • 85
  • 1
  • 12