0
\$\begingroup\$

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?

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

I'll do it this way

In your game class

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

In your game loop

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
\$\endgroup\$
  • \$\begingroup\$ "Simply put time to 0 again to restart the timer." You should add that to the code :) \$\endgroup\$ – Vaillancourt Jan 6 '17 at 14:44
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$ – Vaillancourt Jan 6 '17 at 15:01
  • \$\begingroup\$ That's true, as @AminePaleo said below :) \$\endgroup\$ – Sebastien Servouze Jan 6 '17 at 15:05
2
\$\begingroup\$

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
     }
}
\$\endgroup\$
  • \$\begingroup\$ so it should not be "final"? \$\endgroup\$ – AminePaleo Jan 6 '17 at 14:49
  • \$\begingroup\$ oh, ok, that's true \$\endgroup\$ – AminePaleo Jan 6 '17 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.