# Setting time for a particular action to be happened-LibGdx

In my LibGdx game,enemies are coming in to the screen one by one.

I want to make enemeies appear at a particular time after the game starts.

How can I do it efficiently?

• You can use Gdx.graphics.getDeltaTime() to modify your own custom timer(s) for releasing waves of enemies. – Wabbitseason Jan 6 '17 at 9:45

I'll do it this way

float time = 0; // This will contain the time


time += Gdx.graphics.getDeltaTime(); // This will add to your time variable the time between two frames


And something like this when you want to do something

if (time > 5) // If 5s happened
{
// Make ennemy appear
}


This would do the trick ! Simply put time to 0 again to restart the timer.

time -= 5; // Restart the timer by substract of elapsed time

• "Simply put time to 0 again to restart the timer." You should add that to the code :) – Vaillancourt Jan 6 '17 at 14:44
• In fact, you should set time to time -= 5;. The way it is now, you'll get, on average, less than one enemy per 5 seconds as you'll 'lose' a few ms every time. By reducing the time by 5 seconds, you keep these few ms for the next enemy :) – Vaillancourt Jan 6 '17 at 15:01
• That's true, as @AminePaleo said below :) – Sebastien Servouze Jan 6 '17 at 15:05

you could use delta time

for example :

private final static float TIME_OUT = 5;
private float time = 0;

private update(){
time += Gdx.graphics.getDeltaTime();
if (time > TIME_OUT){
attack();
time -= TIME_OUT; // or set to 0 to repeat the process
}
}

• so it should not be "final"? – AminePaleo Jan 6 '17 at 14:49
• oh, ok, that's true – AminePaleo Jan 6 '17 at 14:50