Timer restarts at certain time

edited November 2014 in How To...

I am trying to implement a timer into my code so that after a certain time it restarts. However, I want the timer to be hidden until it reaches the last three seconds.

So far, I know I should use else and else if, and I'm working on a pseudocode kind of thing. I would like to know if I'm on the right track!

I have a class called Timer, and inside I have a void restart. In void restart, I have if ((timeSoFar == 5){ start(); }

Then in the main code, I call it in void draw. However, whenever it reaches 5, it does not restart but instead keeps counting. I have declared timeSoFar as 0. The code works when I use KeyPressed but I can't seem to make it do it automatically. Before I move onto it showing for the last three seconds, I want to make the timer go by itself.

Any help is appreciated! Thank you.

Tagged:

Answers

  • Answer ✓
    int resetAt;
    color c;
    
    void setup(){
      size(220,220);
      noStroke();
      smooth();
      reset();
    }
    
    void reset(){
      c = color(random(128,255),random(128,255),random(128,255));
      resetAt = millis() + 10000;
    }
    
    void draw(){
      background(0);
      fill(c);
      ellipse(mouseX-10, mouseY-10, 20, 20);
      fill(255);
      textSize(12);
      int timeLeft = resetAt - millis();
      text(timeLeft, 20, 20);
      if(timeLeft<3000){
        textSize(30);
        int countDown = 1 + (timeLeft / 1000);
        text( countDown, width/2, height/2);
      }
      if( millis() >= resetAt ){
        reset();
      }
    }
    
  • It is nice to describe your code, but it would be nicer to show it... If it is too long or off-topic, try to isolate the code specific to the timer in a simpler sketch.

  • Thank you TfGuy44 and PhiLho for your help and inquiry! TfGuy44 has helped me with the timer.

    Have a good day!

Sign In or Register to comment.