How to control the game time-line.

I asked this question [How do you control game speed?]http://www.java-gaming.org/topics/how-do-you-control-game-speed/32898/view.html, and it makes some ambiguity. So I changed the title as above shows.

The question is that, let me take a example, if there is a wizard and he is chanting for a spell, after 2s in real-time the spell should be completed. So how do you measure this interval. If you set down the start time st, and use current time ct to subtract st, it works. However, this implementation would influenced by lagging.

What is the better solution.

What do you mean by lagging ?
That the spell could last a few ms longer than expected ?

somewhere inside your wizards update, work out how to fit it into your code but something like this can detect if 2 seconds has past.


	long oldTime = 0; //set oldTime to when your wizard started the cast
	long timeDelay = 2000; //2000 milliseconds is 2 seconds

	if(System.currentTimeMillis() > oldTime + timeDelay){
		//your wizard has completed your 2 second cast
	}

EDIT: If you want to be more precise, you could use nano time instead of milliseconds, but I don’t think you after precise timing, you just want it to work.

I mean if the system lags, the display or CPU cannot keep up, I would like the game to slow down for a while as many games do. Also, if you paused the game for a while, use ct - st won’t be a good idea.

Same thing with your propose, what about I paused the game?

I highly doubt you will be having a problem with the CPU not being able to keep up, especially if it is 2D which I am thinking it is.

If you are having a CPU problem, I think you may be doing something horribly wrong.

To pause the game, in your game loop, have a if statement around your update, and only let the game update if the game is not paused.

Don’t do it with systems time. Use your in game update timer. Each update, increase your timer by 1. That way even if you drop UPS, everything will run at same game speed.

if you pause the game, ‘System.currentTimeMillis() > oldTime + timeDelay’ this statement will always be true and the spell cast once you resume

true, trollwarriors method would be better if your having a pausing within the game.

How do you usually set your update interval? And to change game speed you use this timer and a global delta?