I'd like to run a function 60 times a second. I prefer to use Util timers which I see a very simple method
int second = 1000;
int fpsLimit = 60;
Timer timer = new Timer();
TimerTask task = new TimerTask(){
public void run(){
//
}
}
timer.scheduleAtFixedRate(task, 0, second / fpsLimit);`
but I see a lot of people use game loops, which are much longer and more complex.
long lastime = System.nanoTime();
double AmountOfTicks = 30;
double ns = 1000000000 / AmountOfTicks;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();
while(isRunning == true) {
long now = System.nanoTime();
delta += (now - lastime) / ns;
lastime = now;
if(delta >= 1) {
Update();
Render();
frames++;
delta--;
if(System.currentTimeMillis() - time >= 1000) {
System.out.println("fps:" + frames);
time += 1000;
frames = 0;
}
}
}
and sorry, since I don't really understand gameloops, are there any advantages of using them other than tracking the fps?