Loading...
Logo
Processing Forum
hi guys,

i got one small question. i want a timer to start and count in my program after a start it. i got an opening screen and if i press enter it goes to the main screen and i want the timer to start then instead of the beginning of the program. i made an small example of what is in my code. maybe you guys can help me.

here is my code:

Copy code
    Copy code
    1. int begintime = 5;
    2. int window = 0;

    3. void setup() {
    4.   size(400, 400);
    5.   background(0);
    6. }

    7. void draw() {
    8.   if (window == 0) {
    9.     background(#FF0000);
    10.   }
    11.   if(window == 1) {
    12.     background(#FFFFFF);
    13.     int sec = frameCount/60;
    14.     int timer = begintime - sec;

    15.     if (timer <= 0) {
    16.       window = 2;
    17.     }
    18.   }

    19.   if(window == 2){
    20.    background(0);
    21.   }
    22. }

    23.   void keyPressed() {
    24.     if (keyCode == ENTER) {
    25.       window =1;
    26.     }
    27.   }
    Thanks in advance

    Replies(4)

    frameCount/60 is not a good idea in case your program get's so heavy that it's under 60 frames.
    You could store the start, millis() gives the amount of millis back since the program started:

    Copy code
    1.     if (keyCode == ENTER) {
    2.       window =1;
    3.       start = millis();
    4.     }

    Then to get how many ms have passed get the millis again and substract the start:
    1.     int ms = millis()-start;
    2.     println(ms);

    hope that helps
    could you give an example with my code of what you mean?
    I think i dont understand you because I tried it and it doesnt work.

    thanks
    press enter...
    Copy code
    1. int begintime = 5;
    2. int window = 0;
    3. int start;

    4. void setup() {
    5.   size(400, 400);
    6.   background(0);
    7. }

    8. void draw() {
    9.   if (window == 0) {
    10.     background(#FF0000);
    11.   }
    12.   if (window == 1) {
    13.     background(#FFFFFF);

    14.     int ms = millis()-start;
    15.     println(ms);

    16.     // int sec = frameCount/60;
    17.     int sec = ms/1000;

    18.     int timer = begintime - sec;

    19.     if (timer <= 0) {
    20.       window = 2;
    21.     }
    22.   }

    23.   if (window == 2) {
    24.     background(0);
    25.   }
    26. }
    27. void keyPressed() {
    28.   if (keyCode == ENTER) {
    29.     window =1;
    30.     start = millis();
    31.   }
    32. }

    Thank you very much! you made my day :D