Objects not disappearing when new screen selected

I’m a bit new to processing, and I’m trying to make a game using multiple levels. Levels are either locked or unlocked, and are represented by either a button or a lock. When a button is pressed, the background does indeed change to the right background, but the buttons and lock icons do not disappear.

I’ve tried multiple things, such as using a screen variable and a game_in_progress boolean, but nothing seems to work. If it helps, both the buttons and lock icons are made using the class function.
(sorry for the whole mess of code, I’m not the best organizer)

Lock myLock2;
Lock myLock3;
Lock myLock4;
Lock myLock5;
Lock myLock6;
Lock myLock7;
void setup() {

screen=select_screen;

myLock2 = new Lock(0,60,100+20,50);
myLock3 = new Lock(0,60,160+20,50);
myLock4 = new Lock(0,60,220+20,50);
myLock5 = new Lock(0,60,280+20,50);
myLock6 = new Lock(0,60,340+20,50);
myLock7 = new Lock(0,60,420,50);
mybutton1 = new button(c,1,35,35,43,50);
mybutton2 = new button(c,2,35,35,43+60,50);
mybutton3 = new button(c,3,35,35,43+60*2,50);
mybutton4 = new button(c,4,35,35,43+60*3,50);
mybutton5 = new button(c,5,35,35,43+60*4,50);
mybutton6 = new button(c,6,35,35,43+60*5,50);
mybutton7 = new button(c,7,35,35,43+60*6,50);
//
l[2]=1; // '0' in this case means locked; '1' means unlocked
l[3]=0;
l[4]=0;
l[5]=0;
l[6]=0;
l[7]=0;

}

void draw() {
if(screen==select_screen) {
displayLocks();
testButtons();}
//displayMainScreen();  //working on other screens right now


}

void displayLocks() {
if(screen==select_screen) {
if(select==false) {
//locks being displayed if l[x] = 0;locked  if l[x] = 1; unlocked and level select buttons
if(screen==select_screen) {
mybutton1.display();}
if(screen==select_screen) {
if(l[2]==0) {
    myLock2.display();
} 
else {
mybutton2.display();
}}
if(screen==select_screen) {
if(l[3]==0) {
    myLock3.display();
}
else {
mybutton3.display();}
}
if(screen==select_screen) {
if(l[4]==0) {
    myLock4.display();
    }
    else {
mybutton4.display();}
}
if(screen==select_screen) {
if(l[5]==0) {
    myLock5.display();
}
else {
mybutton5.display();}
}
if(screen==select_screen) {
if(l[6]==0) {
    myLock6.display();
}
else {
mybutton6.display();}
}
if(screen==select_screen) {
if(l[7]==0) {
    myLock7.display();
}
else {
mybutton7.display();}
}
}
}}

boolean select=false;

void testButtons() {
if(mousePressed) {
if(((mouseX > 43) &&  (mouseX < 78)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    level1();
    select==true;
    } else
    if(((mouseX > 103) &&  (mouseX < 138)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[2]!=0) {
    level2();select==true;}} else
if(((mouseX > 163) &&  (mouseX < 198)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[3]!=0) {
    level3();select==true;}} else
if(((mouseX > 223) &&  (mouseX < 258)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[4]!=0) {
    level4();select==true;}} else
if(((mouseX > 283) &&  (mouseX < 318)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[5]!=0) {
    level5();select==true;}} else
if(((mouseX > 343) &&  (mouseX < 378)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[6]!=0) {
    level6();select==true;}} else
if(((mouseX > 403) &&  (mouseX < 438)) 
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[7]!=0) {
    level7();select==true;}}}}





button mybutton1;
button mybutton2;
button mybutton3;
button mybutton4;
button mybutton5;
button mybutton6;
button mybutton7;
button mybuttonstart;
button mybuttonprog;
button mybuttonbackprog;


//class button
//
class button {
    color c;
    int w;
    float xpos;
    float ypos;
    int size;
    color p;
    int size2;
    boolean func;
    //arguments
    button(color tempc,int tempw, int tempsize, int tempheight, float tempxpos, float tempypos) {
        c = tempc;
        w = tempw;
        p = c;
        xpos = tempxpos;
        ypos = tempypos;
        size = tempsize;
        size2 = tempheight;
    }
    
    void display() {
    
    noStroke();
    rectMode(CORNER);
    if(((mouseX > xpos) && (mouseX < xpos + size)) &&
    ((mouseY > ypos) && (mouseY < ypos + size2))) {
    fill(51);
    } else {
    fill(c);}
    stroke(0);
    strokeWeight(1.5);
    rect(xpos,ypos,size,size2);
    fill(255);
    text(w,xpos+size/1.6,ypos+size2/2);
}}




////   levels themselves
////
////

float player_paddleX=width-45;
float ai_paddleX=45;
float player_offset=-15;  //how much to add/subtract for collision detection 
float ai_offset=15;  //as paddles will be drawn using rectMode(CENTER);
float player_paddleY=mouseY;
float ai_paddleY=width/2-paddle_height/2;
int paddle_height=85;
float ai_distance_from_ball;
float ballX=width/2;
int ballsize=20;
int x;
int y;
//
float dx=-1;   //x direction of ball -1 = left +1 = right
float dy=1;    //y direction of ball -1 = up +1 = down
boolean ai_win=false;     //did the ai win by scoring 2?
boolean player_win=false;   //did the payer win by scoring 2?
int player_score=0;

int ai_score=0;
int max_score=2;

int[] l ={
    2,3,4,5,6,7
    };        //which levels are locked/unlocked?

///


int[] level = {     //used for displaying different levels w/different difficulties
    1,2,3,4,5,6,7
}; 
int Xlevel=0;
int current_level = {    //used for testing whether the next level is unlocked(current BEST level,
    1,2,3,4,5,6,7       //not level currently playing on
};
int Ylevel;
boolean game_in_progress=false;

float[] ball_speed = {
    0.4,0.55,0.625,0.675,0.725,0.775,0.85  //speed of ball per level
};
float t;
float[] ai_easing_spd = {   //speed of computer player paddle per level
    0.045,0.0525,0.0575,0.0635,0.075,0.0775,0.08
};
float ease;

color[] ball_color = {    //dif. ball colors per level
    #d71c1c,#227dd5,#d4d71c,#ef30c2,#d7881a,#1eb816,#b3a019
};


///
void level1() {
gameStart();
screen=game_screen;
Xlevel=1;
t = ball_speed[1];screen!=select_screen;
ballcolor=ball_color[1];
 ease = ai_easing_spd[1];}

void level2() {
 gameStart();
screen=game_screen;screen!=select_screen;
t = ball_speed[2];
ease = ai_easing_spd[2];
ballcolor=ball_color[2];

Xlevel=2;}

void level3() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[3];
float ease = ai_easing_spd[3];
ballcolor=ball_color[3];

Xlevel=3;}

void level4() {
gameStart();
screen=game_screen;
screen!=select_screen;
float t = ball_speed[4];
float ease = ai_easing_spd[4];
ballcolor=ball_color[4];
Xlevel=4;}

void level5() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[5];
float ease = ai_easing_spd[5];
ballcolor=ball_color[5];
Xlevel=5;}

void level6() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[6];
float ease = ai_easing_spd[6];
ballcolor=ball_color[6];
Xlevel=6;}

void level7() {
gameStart();
screen=game_screen;
select_screen!=screen;
t = ball_speed[7];
ease = ai_easing_spd[1];
ballcolor=ball_color[7];
Xlevel=7;}


void gameStart() {
game_in_progress ==true;
background(0);
float s=0;
float w=0.0152;
//x=x+dx*(5*t);
//y=y+dy*(8*t);
if(Xlevel=1) {
ballcolor=ball_color[1];}
if(Xlevel=2) {
ballcolor=ball_color[2];}
if(Xlevel=3) {
ballcolor=ball_color[3];}
if(Xlevel=4) {
ballcolor=ball_color[4];}
if(Xlevel=5) {
ballcolor=ball_color[5];}
if(Xlevel=6) {
ballcolor=ball_color[6];}
if(Xlevel=7) {
ballcolor=ball_color[7];}
fill(ballcolor);
ellipse(x,y,ballsize,ballsize);
text(""+ease,200,100);
}
1 Like

You can add a if statement with the select boolean around the myButtonN.display(); lines. Same should do for the lock icons. Also, take a look at Arrays. It would make your Code a lot easier to read and work with.

1 Like

Thanks, I’ll try that

Always start draw() with background (0); and draw everything afterwards (rule of thumb)

I’ve done that, and it seems to work- but now, they only disappear and the function calls when the mouse is over a button. Is there a way to disable mybuttonN.display();? I tried the select boolean returning false when the mouse is clicked over a button, it didn’t seem to work, however.

EDIT; I’m aware I only turn select_screen off for the first button, the first one is all I’m testing right now.

;

Lock myLock2;
Lock myLock3;
Lock myLock4;
Lock myLock5;
Lock myLock6;
Lock myLock7;
void setup() {
screen=select_screen;
myLock2 = new Lock(0,60,100+20,50);
myLock3 = new Lock(0,60,160+20,50);
myLock4 = new Lock(0,60,220+20,50);
myLock5 = new Lock(0,60,280+20,50);
myLock6 = new Lock(0,60,340+20,50);
myLock7 = new Lock(0,60,420,50);
mybutton1 = new button(0,1,35,35,43,50);
mybutton2 = new button(0,2,35,35,43+60,50);
mybutton3 = new button(0,3,35,35,43+60*2,50);
mybutton4 = new button(0,4,35,35,43+60*3,50);
mybutton5 = new button(0,5,35,35,43+60*4,50);
mybutton6 = new button(0,6,35,35,43+60*5,50);
mybutton7 = new button(0,7,35,35,43+60*6,50);
//
l[2]=1; // '0' in this case means locked; '1' means unlocked
l[3]=0;
l[4]=0;
l[5]=0;
l[6]=0;
l[7]=0;
}
void draw() {
if(screen==select_screen) {
background(170);}
if(screen==select_screen) {
displayLocks();
testButtons();}
//displayMainScreen();  //working on other screens right now

}
void displayLocks() {
if(screen==select_screen) {
if(select==false) {
//locks being displayed if l[x] = 0;locked  if l[x] = 1; unlocked and level select buttons
if(screen==select_screen) {
mybutton1.display();}
if(screen==select_screen) {
if(l[2]==0) {
    myLock2.display();
}
else {
mybutton2.display();
}}
if(screen==select_screen) {
if(l[3]==0) {
    myLock3.display();
}
else {
mybutton3.display();}
}
if(screen==select_screen) {
if(l[4]==0) {
    myLock4.display();
    }
    else {
mybutton4.display();}
}
if(screen==select_screen) {
if(l[5]==0) {
    myLock5.display();
}
else {
mybutton5.display();}
}
if(screen==select_screen) {
if(l[6]==0) {
    myLock6.display();
}
else {
mybutton6.display();}
}
if(screen==select_screen) {
if(l[7]==0) {
    myLock7.display();
}
else {
mybutton7.display();}
}
}
}}
boolean mouseClicked=false;
boolean select=false;

void testButtons() {
if(mousePressed)
  {
    mouseClicked=true;
  }
  if(mouseClicked==true)
  {
if(((mouseX > 43) &&  (mouseX < 78))
    && ((mouseY > 50) && (mouseY < 85))) {
    level1();
    select==true;
    screen!=select_screen;
    } else
    if(((mouseX > 103) &&  (mouseX < 138))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[2]!=0) {
    level2();select==true;}} else
if(((mouseX > 163) &&  (mouseX < 198))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[3]!=0) {
    level3();select==true;}} else
if(((mouseX > 223) &&  (mouseX < 258))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[4]!=0) {
    level4();select==true;}} else
if(((mouseX > 283) &&  (mouseX < 318))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[5]!=0) {
    level5();select==true;}} else
if(((mouseX > 343) &&  (mouseX < 378))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[6]!=0) {
    level6();select==true;}} else
if(((mouseX > 403) &&  (mouseX < 438))
    && ((mouseY > 50) && (mouseY < 85))) {
    if(l[7]!=0) {
    level7();select==true;}}}}


button mybutton1;
button mybutton2;
button mybutton3;
button mybutton4;
button mybutton5;
button mybutton6;
button mybutton7;
button mybuttonstart;
button mybuttonprog;
button mybuttonbackprog;

//class button
//
class button {
    color c;
    int w;
    float xpos;
    float ypos;
    int size;
    color p;
    int size2;
    boolean func;
    //arguments
    button(color tempc,int tempw, int tempsize, int tempheight, float tempxpos, float tempypos) {
        c = tempc;
        w = tempw;
        p = c;
        xpos = tempxpos;
        ypos = tempypos;
        size = tempsize;
        size2 = tempheight;
    }
    
    void display() {
    
    noStroke();
    rectMode(CORNER);
    if(((mouseX > xpos) && (mouseX < xpos + size)) &&
    ((mouseY > ypos) && (mouseY < ypos + size2))) {
    fill(51);
    } else {
    fill(c);}
    stroke(0);
    strokeWeight(1.5);
    rect(xpos,ypos,size,size2);
    fill(255);
    text(w,xpos+size/1.6,ypos+size2/2);
}}

That‘s because for some reason you are setting an unneccessary variable mouseClicked to true when mousePressed is true, but you never set it to false.

So the program only know that mouseClicked is true after you pressed the mouseButton, even if you already released it Long ago.

Just remove that mouseClicked part and replace it with mousePressed, which does exactly what you need.

If you then still need that mouseClicked you can just set it within the if (mousePressed) statement, but don‘t use it to check if the mouse is clicked right now, but only to check if the mouse was ever clicked.

Or set the mouseClicked to false when the mouse is released… although that‘s pretty unneccessary.

That’s because when I used mousePressed for the button, the user would have to continuously hold down the mouse in order to keep the function running. My problem is that once it’s clicked, the background only goes to black when the mouse is over a button.

Well, for starters, try changing all those individual variables into arrays. That would make a lot of things easier.

Also, use a for loop in your displayLocks method (and others). As for the Lock class, i can‘t find it in the Code you posted…

After that it should be a lot easier to find where the issue is.

And maybe try to use a boolean for your Levels, to check which one is selected. Then instead of calling that function in testButtons, call it in another function that checks wether the Levels boolean is true and then calls the respective method…

I tried the boolean game_in_progress for that purpose, and used it where the select boolean is tested and it did not seem to work. By the ‘individual variables’ do you mean the location of the buttons/locks?

here’s the lock class;

class Lock {
    color r;
    color c;
    float xpos;
    float ypos;
    
    //arguments
    Lock(color tempc, color tempr, float tempxpos, float tempypos) {
        c = tempc;
        r = tempr;
        xpos = tempxpos;
        ypos = tempypos;
    }
    
    void display() {
    noFill();
    strokeWeight(7.5);
    stroke(r);
    ellipse(xpos,ypos,20,20);
    noStroke();
    fill(c);
    rectMode(RADIUS);
    rect(xpos,ypos+17.5,17.5,17.5);
    }
}

It‘s very hard to read your Code, so i can‘t really find where the problem lies exactly.

So for now i‘ll just show you how your code could look like to achieve what i think you want to achieve (to continuisly activate the LevelN() method, right?) :

Button[] myButtons;

void setup () {
   size(800,600);

   myButtons = new Button[7];

   for (int i = 0; i < myButtons.length; i++) {
      myButtons[i] = new Button(100 + (i * 10), 500, 60, 20);
   }

}

void draw() {
background(255);
drawButtons();
checkButtonsPressed();
activateLevel();
}

void drawButtons() {
   for (int i = 0; i < myButtons.length; i++) {
      myButtons[i].display();
   }
}

void checkButtonsPressed() {
   for (int i = 0; i < myButtons.length; i++) {
      myButtons[i].checkPressed();
   }
}

void activateLevel() { //apparently you have to constantly call levelN(); to play the Level?
   for (int i = 0; i < myButtons.length; i++) {
      if (myButtons[i].getActivated()) {
         level(i);
      }
   }
}

void level (int i) {
   switch (i) {
      case 1 : 
         level1();
         break;
      case 2 : 
         level2();
         break;
      case 3 : 
         level3();
         break;
 // you get the point
   }
}

class Button {
   PVector pos;
   PVector size;
   boolean activated; //to have a toogle or oneclick button
   boolean locked; // no need for an extra lock class...

   //You can add more variables like color, but i kept it simple for now

   Button (float x, float y, float w, float h) {
      pos = new PVector(x, y);
      size = new PVector(w, h);
      activated = false;
      locked = true;
   }

   void display() {
      rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y);
   }

   void checkPressed () {
      if (mouseX > pos.x && mouseX < pos.x + size.x && mouseY > pos.y && mouseY < pos.y + size.y) {
            if (mousePressed) 
              activated = true;
        }
   }

   void toogleLocked() {
      locked = !locked;
   }

   boolean isActivated() {
      return activated;
   }

   boolean isLocked() {
      return locked;
   }
}

I’ve looked at this code, and I see that void checkButtonsActivated is missing after being called in void draw();

Indeed, i replaced the variable name and forgot to change it in draw. It’s activateLevel().

I see that in a number of places you seem to have made a coding error. A single equals sign (=) assigns a value and a double equals (==) tests equality.

The following does not work as it seems you intended.

select_screen==false;

What the above statement actually does is test if select_screen is false. It does not assign any value to anything, and the result is merely discarded. To assign a value you would need to do this:

select_screen = false

An important note is that the first bit, which checks equality but does nothing with the result, is valid code even though it doesn’t assign the value.

P.S.

You cannot do a negation assignment as below:

screen!=select_screen

The above is a another conditional check, this time to see if the variable does not equal the other variable.

2 Likes