How to make a timer in processing??

edited February 2018 in How To...

Hi I'm making a game, and i need a timer in the game and I cannot figure out how to make one. Can someone help me make one?

Tagged:

Answers

  • Answer ✓

    You can install the TimingUtilities using the Library manager in Processing. A link to the lib in github: https://github.com/Lord-of-the-Galaxy/Timing-Utilities

    Kf

  • Thanks! Do you know how to make a timer in seconds?? :-/

  • Answer ✓

    From the github example:

    import lord_of_galaxy.timing_utils.*;
    
    Stopwatch s;//Declare
    
    void setup(){
      size(640, 360);
    
      //Creating a stopwatch to keep time
      s = new Stopwatch(this);
    
      //Start the stopwatch
      s.start();
    } 
    
    void draw(){
      background(0);
      fill(255);
    
      textSize(50);
    
      //Use s.time() to get current time in milliseconds
      text(str(s.time()), 50, 100);
    }
    

    To get the time in seconds for the last line:

    text("Time in secs: "+str(s.time()/1000.0), 50, 100);

    To format the number, look at round(), floor(), nf() in the reference.

    Kf

  • Thank you so much!!

  • edited February 2018 Answer ✓

    If all you need is just to set a wait time for some event and know when it's finished, you can use the Countdown class from the link below instead: O:-)

    https://Forum.Processing.org/two/discussion/23846/time-delay-in-python-mode#Item_11

    An online example btW: :bz
    http://Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60

    1. Instantiate the Countdown class passing the desired wait time in milliseconds.
    2. Then call its start() method so the countdown begins.
    3. Keep an eye when its field done turns true. That's it! :-bd

    BtW, you can always start over a Countdown instance by reinvoking its start() method; even when it's not done yet. :ar!

    It simply cancel() its old TimerTask and schedule() a new 1, using its field delay's current value. :>

    And of course, you can have more than 1 Countdown instance at the same time; so you can track down more than 1 event w/ diff. wait times if your sketch demands it. *-:)

Sign In or Register to comment.