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

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))

                if(camTimer.ElapsedMilliseconds >= 2000.0f)
                    camera.CameraInRange = false;
                else if (camTimer.IsRunning)
                    camera.CameraInRange = true;
share|improve this question
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). – Andrew Russell Jul 3 '11 at 11:50
I do not see how this is off-topic, Mr. Vote-to-close. – The Communist Duck Jul 3 '11 at 14:54
There's nothing game-related about it. It's a basic failure to understand C# object creation, Stopwatch is not an XNA class. – user744 Jul 3 '11 at 15:45
@Joe is right, this is not the right place to teach basic programming skills – Maik Semder Jul 3 '11 at 16:09
(However, it is a clear question with a specific answer, so I didn't downvote it.) – user744 Jul 3 '11 at 17:12
up vote 5 down vote accepted

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(){

    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

share|improve this answer
Why would anyone downvote this? – Jonathan Connell Jul 4 '11 at 10:50
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. – user744 Jul 4 '11 at 13:36

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.