1
\$\begingroup\$

I've got a timer in my game which is supposed to set off a Boolean back to false after a certain amount of time has passed it just doesn't seem to work despite me trying to debug the issue, although setting off the timer works perfectly fine. Any help with this would be much appreciated

for (int i = 0; i < cameras.Count; ++i)
            {
                DetectCamera camera = cameras[i];
                Stopwatch camTimer = new Stopwatch();

                if (player.InterferenceStart && player.EMPBoundingCircle.Intersects(camera.BoundingRectangle))
                    camTimer.Start();

                if(camTimer.ElapsedMilliseconds >= 2000.0f)
                {
                    camera.CameraInRange = false;
                    camTimer.Reset();
                }
                else if (camTimer.IsRunning)
                    camera.CameraInRange = true;
            }
\$\endgroup\$
16
  • 3
    \$\begingroup\$ Normally in XNA you'd use GameTime.ElapsedGameTime in your Update function to provide timing (to count up some time, for a delay, you'd accumulate it in a variable). \$\endgroup\$ Commented Jul 3, 2011 at 11:50
  • \$\begingroup\$ I do not see how this is off-topic, Mr. Vote-to-close. \$\endgroup\$ Commented Jul 3, 2011 at 14:54
  • 1
    \$\begingroup\$ There's nothing game-related about it. It's a basic failure to understand C# object creation, Stopwatch is not an XNA class. \$\endgroup\$
    – user744
    Commented Jul 3, 2011 at 15:45
  • \$\begingroup\$ @Joe is right, this is not the right place to teach basic programming skills \$\endgroup\$ Commented Jul 3, 2011 at 16:09
  • \$\begingroup\$ (However, it is a clear question with a specific answer, so I didn't downvote it.) \$\endgroup\$
    – user744
    Commented Jul 3, 2011 at 17:12

1 Answer 1

5
\$\begingroup\$

You declare camTimer inside a method, as soon as your code exits this method the timer will be stopped and garbage collected. Also Your code probably calls this method every frame, so camTimer is created newly everyframe, so the time is never more than a few miliseconds.

Both of these problems can be solved by moving the camTimer out of the method and into the class containing the method. For example:

public class MyClass()
{
    StopWatch camTimer;

    public MyClass(){
        camTimer = new camTimer();
    }

    public void StartTimer(){
        camTimer.Start();
    }

    public void StopTimer(){
        camTimer.Reset(); //stops and sets time at 0, alternatively use stop to not set at 0 but keep time
    }

    public void DoSomethingWithCameras(){
            ...
            ...
            if(camTimer.ElapsedMilliseconds > 2000.0f){
                ...
                camTimer.Restart(); //starts measuring from 0 again

            }
            ...
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Why would anyone downvote this? \$\endgroup\$ Commented Jul 4, 2011 at 10:50
  • \$\begingroup\$ I downvote answers to offtopic or duplicate questions to discourage reputation farming. I think it's entire fair to call answers in the wrong place "not useful", and therefore deserving of -1. \$\endgroup\$
    – user744
    Commented Jul 4, 2011 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .