Hi, I’m doing an assignment which I have to create a game and I’ve attempted to create a functioning menu. I coded buttons that navigate to different modes(?) when clicked using class (to create button) and switch (to go to different modes) and when I click the button, it does change to the mode specified but doesn’t show the output in the specific mode, e.g. clicking on button 1 doesn’t show the contents of mode1()[shown in code below]. I’m not sure why this is happening. I’d be immensely grateful if someone could help me understand and resolve this issue as soon as possible. Thank you!
Here’s my code.
int mode=0;
Button[] buttons;
void setup()
{
size(1400, 780);
textAlign(CENTER);
strokeWeight(4);
buttons = new Button[5];
buttons[0] = new Button(150, 650, "Back", 250, 252, 251);
buttons[1] = new Button(150, 300, "1", 229, 235, 215);
buttons[2] = new Button(150, 400, "2", 247, 215, 215);
buttons[3] = new Button(150, 500, "3", 174, 188, 196);
buttons[4] = new Button(150, 600, "4", 212, 238, 227);
}
void draw()
{
background(226);
switch(mode)
{
case 0:
{
fill(0);
text("Game", 800, 200);
for (int i=1; i<=4; i++) {
buttons[i].display();
buttons[i].update();
if (buttons[i].isClicked()) {
menu=i;
}
}
}
break;
case 1:
{
mode1();
}
break;
case 2:
{
mode2();
}
break;
case 3:
{
mode3();
}
break;
case 4:
{
mode4();
}
break;
case 5:
{
complete();
}
}
}
void back_menu()
{
buttons[0].display();
buttons[0].update();
if (buttons[0].isClicked()) {
menu=0;
}
}
Here’s the class I made for the buttons
class Button {
int x, y;
int w = 150;
int h = 80;
String t;
color c;
boolean over(int x, int y, int w, int h) {
return (mouseX > x & mouseX < x + w &&
mouseY > y & mouseY < y + h) ;
}
boolean Pressed = false;
boolean Clicked = false;
Button (int Bx, int By, String text, int r, int g, int b) {
x = Bx;
y = By;
t = text;
c = color(r, g, b);
}
void update() {
if (mousePressed == true && mouseButton == LEFT && Pressed == false)
{
Pressed = true;
if (mouseX>=x && mouseX <=x+w && mouseY <=y+h)
{
Clicked = true;
}
} else
{
Clicked = false;
}
if (mousePressed !=true)
{
Pressed = false;
}
}
void display() {
if (over(x, y, w, h)) {
stroke(192, 41, 66);
fill(c);
rect(x, y, 150, 80);
pushStyle();
fill(0);
textSize(20);
text(t, x+w/2, y+h/2);
popStyle();
} else {
stroke(0);
fill(c);
rect(x, y, 150, 80);
pushStyle();
fill(0);
textSize(20);
text(t, x+w/2, y+h/2);
popStyle();
}
}
boolean isClicked()
{
return Clicked;
}
}