timer

im making a game and in there is a timer. the thing is its in real time and i want it to start a 0 when the game launches so you can see how long you survived. if you need my code just ask, thanks in advance.

Answers

  • Use millis().

  • As TfGuy44 says:

    millis() https://processing.org/reference/millis_.html

    Returns the number of milliseconds (thousandths of a second) since starting the program. This information is often used for timing events and animation sequences.

  • Hello @cloudi

    This sample uses a timer to start, stop, continue and reset, it doesnt use any class, but works. It's a runner who starts from the left side of the screen and finish whenever you want, you cand set according your need. I hope it works for you

    String str="";
    int timerStart = 0;  
    int offset;        
    float pic1=-70;
    float vp=44.444444;   
    int mill;       
    int minutes;   
    int seconds;    
    int hundredths; 
    boolean stopped = true;
    boolean continued = false;
    boolean b1c1 = false;
    PImage c1;
    PImage partialSave;
    
    void setup() {
      size(1000, 600); 
      c1=loadImage("c1.png");  
      partialSave = loadImage("runner1.png");
    }
    
    void draw() { 
      background(255); 
      textSize(22);    
      timer();
    }
    
    void timer()
    {
      image(partialSave,0,150,70,60);
      image(partialSave,pic1,230,70,60);
       fill(0,0,0);
       textSize(16);
        text("Press i to init runner, p to stop it, c to continue, s to reset data",50,70);
        text(nf(minutes,2,0)+":"+nf(seconds,2,0)+":"+nf(hundredths,2,0),50,90);
      if(!stopped) 
      {
        mill=(millis()-timerStart);
        if(continued) mill += offset;
    
        textSize(14);
        seconds = mill / 1000;  
        minutes = seconds / 60;
        hundredths = mill / 10 % 100;   
        pic1=((vp*(hundredths+(seconds*100)))/100)-70; 
        image(partialSave,pic1,230,70,60);   
      }
    }
    
    void keyPressed()
    {
        if(key=='p'){
          stopc1();
        }
        if(key=='i'){
          initc1();
        }
        if(key=='c'){ 
          continuec1();
        }
        if(key=='s'){ 
          pic1=-70;
        }
    }
    
    void initc1()
    {
      stopped = false;
      continued = false;
      timerStart = millis();
      b1c1=false;
    }
    
    void stopc1()
    {
      stopped = true;
      b1c1=false;
    }
    
    void continuec1()
    {
      stopped = false;
      continued = true;
      timerStart = millis();
      offset = mill;
    }
    
  • Not sure if this is what you want, but you might want to look at the java.util.Timer class.
    There also are many existing Stopwatch classes in Java, as can be seen in this post on stackoverflow.com.

  • There are also two timer libraries for Processing available from the Libraries page and through Contributions Manager:

    1. CountdownTimer by Dong Hyun Choi

      A countdown timer which triggers callback events for each user-defined tick interval during the timer's duration.

    2. TimedEvents by Jason Gessner

      A couple of classes for firing off timed events at regular or random intervals.

  • So these Processing libraries are synchronous or asynchronous? As in, do they call the callback function inside draw, or as soon as the time gets up?

Sign In or Register to comment.