Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Generally, we associate a "state time" with our sprites. Libgdx provides a method to do so:

float deltaTime =;

However, in the main game loop, we have a delta time as a parameter in render() methods. Suppose my player has states like Idle, WalkLeft, WalkRight, Jump, Hit, and Die. I use the delta time to know how much total time has elapsed within a state:

public void setState (int state){
    float deltaTime =;
    this.state = state;
    this.stateTime += deltaTime;

Is it proper to write this, or I should take the parameter for delta time from the method that calls it?

share|improve this question
up vote 6 down vote accepted

I think you're conflating render and logic updates. The delta returned by the object is an elapsed time value that probably should only be used for render updates, but the state an object is in is more of a game logic issue, and you generally want these things handled on different timers.

This is because if you pause your game logic (by stopping the game logic timer, so all delta times for logic are zero), you may still want some aspects of your rendering (such as, perhaps, HUD animations) to continue.

So first I would recommend you divorce your graphics and logic timers. Then, I would recommend pushing the delta time through to every object that needs it as a parameter. You can still your delta time by calling, but do it only once at the top-most update function, and push the resulting delta down through every game or render object's own update methods. This helps decouple them from the global instance, allowing you to do better global control of timing-related effects (like slowing everything down, or pausing things as described above).

I'd also recommend moving the update of the elapsed time into an actual update() method, and not doing it in setState. It makes very little sense that setting the state would also advance the object's simulation time, and it means you can't call setState more than once in an update interval without artificially pushing the clock for that object forward.

share|improve this answer
Thanks Josh. I was expecting such a detailed answer. – Vicky Jun 20 '13 at 3:28 returns the same delta for each frame.

Running this code will never return "Delta different" even though it does get called at different times.

    float delta =;

    for(int i = 0; i <= 1000000 ; i++)
        if(delta !="Test", "Delta different");
share|improve this answer
That is because it gives the delta time for the last frame. Next frame it will give you a different delta (if you're not running in fixed time-step). As with any timestep related question see : – Roy T. Jun 19 '13 at 15:51
I know that. I meant that inside a single frame the Delta Time will always be the same. – mobo Jun 19 '13 at 16:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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