1

I have a couple different enemy types and enemyManager arrayList classes for each type. I have each type of enemy randomly spawning at spawn points slightly off screen, coming onto the screen, then off the other side, dying, and randomly responding. The problem is when I use a loop to spawn the objects many of them spawn in the same place or catch up with each other die around the same time and spawn again. I would like to have a delay between them so they are more spread out.

What I am looking for is a way to slow down the looped spawning of enemies in java. I tried extending enemy manager classes by timer and naming the spawn function run but this didn't work.

Also I am not multithreading because I don't really know how to set that up yet and was trying to finish this project without implementing that, but if that seems like the best solution then I guess I will have to.

thanks for any suggestions.

updated .....

class spawnLgCars extends TimerTask {
    public void run() {
        if (lgCars.size() < 10) {
            lgCars.add(new LgCar());
            System.out.println("spawned");
        } else if (lgCars.size() > 10) {
            lgCars.get(0);
        }
    }

}

Here I how I'd like to implement TimerTask, but because it had to be in it's own class it didn't have access to the properties of the instance of lgCars I was using. Then I tried adding extending lgCars by Timer Task and calling the task in the constructor, but this also didn't work. not sure why.

Dakota Hipp
  • 749
  • 7
  • 16
  • "but this didn't work" tells us little. In fact, I am having a great deal of difficulty trying to figure out just what is causing your current problems. Please provide more background, detail, and pertinent code. What GUI library are you using? If Swing, then use a Swing timer for your delay. If not, then possibly a java.util.Timer or scheduled executor service. And yeah, it's probably time for you to read up on threading. – Hovercraft Full Of Eels Oct 08 '14 at 21:33
  • I tried Thread.sleep but it didn't work, it was pausing my whole program rather than just the spawning in the enemy manager. I am using swing and awt. most of what I added I've found on various tutorials. This is my first time building a gui in java. I'm using a swing timer to repaint everything. I am not sure if I can use another one to spawn enemies? I'll post how I tried to implement TimerTask below and explain what it did and why it wouldn't work. Thanks both of you. – Dakota Hipp Oct 08 '14 at 22:04

1 Answers1

0

TimerTask and a java.util.Timer won't work because it is not set up to run the repeated code on the Swing event thread and should be avoided with Swing GUI's. Again you should use a Swing Timer since all the code that is called in the Timer's ActionListener is called on the Swing event thread.

On the other hand, if you have a long running task, such as if you wanted to do image analysis or something else that takes a long time to run, then that should be called in a background thread such as via a SwingWorker.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373