1

I am trying to make a game where an enemy object spawns every 3 seconds and tries to chase your player, but I don't know how I will make a generator that creates an enemy object every 3 seconds.

I already tried this by creating a thread that runs every 3 seconds, but it gives me an error that says

No OpenGL context found in the current thread.

I'm using OpenGL to program displays and textures.

Pang
  • 9,564
  • 146
  • 81
  • 122
  • Don't use `Thread` for this purpose. It is use case of `javax.swing.Timer`. Try some code with timers, and if you had any problem post your code here. Without a code there is little chance for getting good answers. – STaefi Jan 31 '16 at 08:44
  • Timer can provide an event every `n` second, and in the listener of that event, you can make your spawns instances. – STaefi Jan 31 '16 at 08:45
  • thanks! I'll try to search about timers and try to use them. :) – Daniel Song Jan 31 '16 at 09:57
  • oh, and i don't think i can add the code here because it has alot of classes and its VERY messy... but ill try my best to figure out how i should do it! :D – Daniel Song Jan 31 '16 at 10:00
  • Possible duplicate of [No OpenGL context is current in the current thread](http://stackoverflow.com/questions/30992781/no-opengl-context-is-current-in-the-current-thread) – Solomon Slow Jan 31 '16 at 14:49
  • my problem is a little bit different because i need multiple threads that needs to create the enemies (there will be multiple types of enemies) but luckily i found my own way to fix this problem thanks to the other two people :) – Daniel Song Feb 01 '16 at 04:12

1 Answers1

0

You can use a ScheduletExecutorService and schedule at fixed rate every 3 seconds. It will not create a new thread every 3 seconds, instead it uses only one additional thread to create all the objects you need.

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
    public void run() {
       // Create your object;
    }
}, 0, 3, TimeUnit.SECONDS);

To see more:

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56