Time delay issue

I’ve just started learing how to program in processing a couple of days ago and the nxtlvl function gets to be called when 5 points are reached, which means a level is completed. My problem is that the pause() function i created always runs before any of the code used above it in the function. As far as i know the code is supposed to be read from top to bottom, and that’s why I have no clue why pause is running before the “Level 2” screen

Here’s the code:

void setup(){
  size(500, 500);
  background(0);
  frameRate(60);
}
int pts = 0;
float size = 25;
float check = 0;
float [] xspd = {random(3,5),random(3,5),random(3,5),random(3,5),random(3,5)};
float [] yspd = {random(3,5),random(3,5),random(3,5),random(3,5),random(3,5)};
float [] stp = {0,0,0,0,0};
float [] x = {random(25, 475), random(25, 475), random(25, 475), random(25, 475), random(25, 475)};
float [] y = {random(25, 475), random(25, 475), random(25, 475), random(25, 475), random(25, 475)};
int lvl = 1;
int clicks = 0;
void draw(){
  background(0);
  textSize(20);
  fill(255, 0, 0);
  text("Points: " + pts, 5, 20);
  textSize(20);
  fill(255, 255, 0);
  text("clicks: " + clicks, 400, 20);
  textSize(35);
  fill(255,0,0);
  text("Level " + lvl, 180, 35);
  for(int i = 0; i < 5; i++){
    fill(random(0, 255), random(0, 255), random(0, 255));
    stroke(random(0, 255), random(0, 255), random(0, 255));
    ellipse(x[i], y[i], size, size);
    if(stp[i] == 0){
      x[i] = x[i] + xspd[i];
      y[i] = y[i] + yspd[i];
    }
    if(x[i] > width - 12.5 || x[i] < 12.5){
      xspd[i] = -xspd[i];
    }
    if(y[i] > width - 12.5 || y[i] < 12.5){
      yspd[i] = -yspd[i];
    }
  }
  if(pts == 5){
    lvl = lvl + 1;
    nxtlvl();
  }
}
//Next Level Screen
void nxtlvl(){
  if(pts == 5){
    background(0);
    textSize(50);
    fill(255, 0 ,0);
    text("Level " + lvl , 150, 255);
    pts = 0;
  }
  pause(2);
}

// points and clicks adder
void mousePressed(){
  clicks = clicks + 1;
  for(int i = 4; i > -1; i--){
    if( ( (mouseX-x[i] < 12.5) && (x[i]-mouseX < 12.5)) && ((mouseY-y[i] < 12.5) && (y[i]-mouseY < 12.5)) ){
      x[i] = -100;
      y[i] = -100;
      pts = pts + 1;
      stp[i] = 1;
      break;
    }
  }
}

//stop the program for x seconds
void pause(int x){
    int te = millis();
    int stopTime = te+(x*1000);
    while(te < stopTime){
      te = millis();
    }
    
}
1 Like

I am aware that no new level is available yet, I want to focus on fixing the time delay problem first

-a- please format your code using the

</> code tag

and

```
type or paste code here
```

and please make it a small little runnable code for the related question / problem ONLY


-b- sorry but delay / wait… is something what should not be used,
esp. not inside draw() …
but it works:

long startT, stopT;

void setup() {
  println("start");
  pause(10);
  println("end timer");
}

void draw() {
}

void pause(int x) {            //stop the program for x seconds
  stopT = startT + x * 1000;
  while (millis() < stopT) { }
}


-c- so using a similar timer without “stopping” the program would be:

long startT=0, dT=5*1000;
boolean resetT = true;
int step= 0;


void myTimer() {
  if ( resetT && millis() > startT+dT) { 
    step++;
    startT += dT;      // restart for next step;
  }
}

void setup() {
}

void draw() {
  background(200, 200, 0);
  myTimer();
  fill(0);
  if ( step == 0 ) draw_step0();
  if ( step == 1 ) draw_step1();
  if ( step == 2 ) draw_step2();
  //..
  draw_info();
}

void draw_step0() {
  text("start timer", 10, 10);
}

void draw_step1() {
  text("end timer", 10, 10);
  //resetT = false;               // here disable!
}

void draw_step2() {
  text("UPS", 10, 10);
}

void draw_info() {
  text("step: "+step+", fps: "+nf(frameRate, 1, 1), 10, height-5);
}

you see with printing FPS, that the program is executing draw() 60 times per sec.

2 Likes