Search code examples
c#xnaxna-4.0

GameTime and real time


I am trying to fill a rectangle in 3 seconds of real time
I want the increment to be a constant value to look nice and not have acceleration
and I am having trouble understanding what to do
this is my code

// constant
// 1.0f = 100% of rectangle, 3 sec = 3000.0 miliseconds
float addValue = 1.0f/3000.0f; 

    public override void Update(GameTime gameTime)
    {
        newGameTime += gameTime.ElapsedGameTime.Milliseconds;

        // once the percentage is set to 0 this starts
        if ((percentage < 1))
        {
            // calculate value here in order to time
            percentage += addValue;
        }
    }

I've been trying all kind of crazy math to get it right but i completely lost it. :( I know I should be using gameTime or newGameTime but I'm lost


Solution

  • I assume thats your update / rendering function.

    Let's say, for example, that since the last rendering, 300ms elapsed. That means you'd have to add 100%/3000ms * 300ms = 10% to your rectangle.

    -> You're missing the elapsed time in the calculation:

    percentage += addValue * gameTime.ElapsedGameTime.Milliseconds;