Search code examples
xna-4.0

Lap timer in XNA 4.0?


Right, I've got a slight problem here, in which I've attempted to implement a lap timer.

In my protect override void update I've got this;

if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
    {
        redHit = true;
        _timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
    }
}

What I'm saying here^ is, if car2red is colliding with the starting line, the timer begins, but if it's not, timer does not add seconds (it just stops_ . How can I make it so, if car2red hits the startingLine and moves forward a few pixels (without touching starting line) the timer still continues?

Thank you.


Solution

  • You should have a separate if statement like this:

    if (redHit) {
        _timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
    }
    
    if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
    {
        redHit = true;
        //Only use this line if you want to reset the timer to 0 when the player crosses that line again.
        _timer1 = 0;// I'm assuming that _timer1 is a double
    }