We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
timers (Read 1339 times)
timers
Mar 16th, 2010, 7:53pm
 
Hello, I'm trying to implement a timer into my code,  but I only want it to show up when I call a certain 'stage' function. So since I'm using the millis(); function, once I get to the screen that I want, the timer has already reached 0. When I want it to countdown from 10 to 0. Here is the code for this part:
Code:

int startingTime;
currentTime = (millis() - startingTime)/1000;
text("Time: " + counter, 70, 50);
       
     if(currentTime <= 1){
       counter = 10;
     }else if(currentTime == 2){
       counter = 9;
     }else if(currentTime == 3){
       counter = 8;
     }else if(currentTime == 4){
       counter = 7;
     }else if(currentTime == 5){
       counter = 6;
     }else if(currentTime == 6){
       counter = 5;
     }else if(currentTime == 7){
       counter = 4;
     }else if(currentTime == 8){
       counter = 3;
     }else if(currentTime == 9){
       counter = 2;
     }else if(currentTime == 10){
       counter = 1;
     }else if(currentTime == 11){
       counter = 0;
     }

That's about it so far..is there a way to reset the value assigned to millis(); later in the program so it doesn't keep incrementing from the start of the program? Thanks!
Re: timers
Reply #1 - Mar 16th, 2010, 8:45pm
 
It's not a problem... unless you plan to run your program for longer than 24 days...

Tongue

(2,147,483,647 milliseconds = 24.8551348 days)
Re: timers
Reply #2 - Mar 16th, 2010, 8:49pm
 
Yeah, but what happens is once I get to this point in the program where I want the timer to start displaying a countdown, it just shows a value of 0.

I don't know if I have to reset the currentTime or millis functions?I have a println of currentTime and it's always 0 until it gets to this stage where I want it to start counting, but then it starts printing out numbers starting at either 50 or 80..it's never consistent, so I'm not sure what the issue is.
Re: timers
Reply #3 - Mar 16th, 2010, 9:24pm
 
I'm sure you can adapt the following to suit your needs:

Quote:
int start;
int count;
void setup(){
  start = millis();
  count = 10;
  println( count );
}

void draw(){
  if( (millis()-start) >= 1000 ){
    start = millis();
    count--; 
    if( count == -1 ){
      count = 10;
    }
    println( count );
  }
}


Grin
Re: timers
Reply #4 - Mar 16th, 2010, 10:17pm
 
hmm, i'm still having some issues with implementing it into the stage..like i see how this prints 10 to 0 in the console, but is there a way that I can print a timer counting down from 10 on the screen? I'm pretty sure that the issue is that millis wasn't resetting once i get to that point in the game, but I'm not entirely sure.
Re: timers
Reply #5 - Mar 16th, 2010, 10:25pm
 
int start;
int count;
void setup(){
 start = millis();
 count = 10;
 println( count );
}

void draw(){
 if( (millis()-start) >= 1000 ){
   start = millis();
   count--;
   if( count == -1 ){
     count = 10;
   }
 }
 text("Time: " + count, 70, 50);
}
Page Index Toggle Pages: 1