Seconds Counter Thing

edited October 2017 in How To...

So im making the most basic thing that I know how to code, a clicker game, and im trying to figure out how to add points every so seconds. So for example one of the things adds one point every second then one it would add 2 points, and im not sure how i would go about doing this. If you have an answer that would be great!

Tagged:

Answers

  • edited October 2017 Answer ✓

    Use millis(). It returns the number of milliseconds since the sketch started. So if you want something to happen every second, first, remember the value returned by millis() at the end of setup(). Then, in draw, check to see if the value that millis() returns then is more than 1000 different. That is, if( millis() > time +1000 ){. If this condition is true, then you know at least one second has passed. In this conditional statement's code block, you can do any actions that should happen every second. And then, finally, remember the NEW value of millis(), so that this conditional statement will become true again in another second.

    int time;
    
    void setup(){
      size();
      /// ...
      time = millis();
    }
    
    void draw(){
      if(millis() > time + 1000 ) { /// 1000 milliseconds or one second.
        // ...
        time = millis();
      }
      // ...
    }
    
  • Thank you so much! This helped so much!.

  • How can my friends play my game that i have made with processing?

Sign In or Register to comment.