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();
}
}