I'm trying to make a mini game where the enemies spawn around the player, the problem is that they come out very fast and very often and the gameplay is very difficult, these enemies are part of an arrayList of "enemy" objects and I wanted to know if there is any method that allows me to slow down its appearance. It is clear that I try to do it in the following way:
Class RunAction:
public class EjecutarAccion implements Runnable{
boolean detenerEjecucion = false;
public void detenerEjecucion(boolean decision){
this.detenerEjecucion = decision;
}
@Override
public void run() {
while(!detenerEjecucion){
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(EjecutarAccion.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Drawing class - Paint method:
Thread hilo = new Thread(ejecutar);
public void paint(Graphics g) {
add(jugador);
super.paintComponent(g);
setBackground(Color.BLACK);
jugador.dibujarJugador(g);
jugador.disparar(g, bala);
hilo.start();
if (enemigos.size() <= 1) {
enemigos.add(new Enemigo());
}
}
As you can see my intention is to wait 5 seconds between the spawneo of each enemy. The problem of doing it this way is that I pause the whole game 5 seconds every time I destroy an enemy and go to spawnear another; that's why I wanted to know if there is a way to do this without affecting the whole game.