Menu Screen Help

I’m having trouble with my menu screen for a game I’m trying to code. When I click the mouse, it isn’t taking me to the next screen (i.e when I click levels it doesn’t show the levels screen.) Can anyone help me? Thank you! (Here’s what I have for the code so far)

int select=1;//start game-1,levels-2,instructions-3

int gameScreen=1;//game-1,levels-2,instructions-3,main menu-4

void setup(){

fullScreen();

}

void draw(){

  textSize(80);

 println(mouseX,mouseY);

 click();

 titles();

}

void titles(){

  background(0);

 text("START GAME",50,250); 

 text("LEVELS",50,550);

 text("HOW TO PLAY",50,850); 

}

void gamePlay(){//MOVE TO NEW TAB WHEN STARTING FOR ALL VOIDS

 background(0);

 text("Start game here", 50,550);

}

void levels(){

  background(0);

  textSize(80);

 text("LEVEL 1",850,150);

 text("LEVEL 2",850,350);

 text("LEVEL 3",850,550);

 text("LEVEL 4",850,750);

 text("LEVEL 5",850,950);

}

void tutorial(){

  text("We will write the tutorial here",50,550);

}

void click(){

  

 if (mouseX>34 && mouseX<563 && mouseY>174 && mouseY<255 && mousePressed){

  gameScreen=1;//game

 }

 if (mouseX>34 && mouseX<328 && mouseY>483 && mouseY<551 && mousePressed){

 gameScreen=2;//levels

 } 

 if (mouseX>34 && mouseX<333 && mouseY>770 && mouseY<858 && mousePressed){

  gameScreen=3;//instructions

 }

}
1 Like

it might be to your benefit to build a button class. here is a simple example.

class Button
{
  float x, y, w, h, padding;
  String label;
  Button(float x, float y, String label) {
    this.padding = 10;
    this.w = textWidth(label) + padding;
    this.h = textAscent() + textDescent() + padding;
    this.x = x - this.w / 2;
    this.y = y - this.h / 2;
    this.label = label;
  }
  
  boolean hitTest(float x, float y) {
    return (this.x < x && this.x + this.w > x &&
            this.y < y && this.y + this.h > y);
  }
  
  void present() {
    fill(100, 0, 100);
    rect(this.x, this.y, this.w, this.h);
    fill(255);
    text(this.label, this.x + this.padding / 2, this.y + textAscent() + this.padding / 2);
  }
}

int state;
Button[] buttons;

void setup() {
  size(640, 480, P2D);
  state = 0;
  buttons = new Button[] {
    new Button(width / 2, height / 2, "New Game") 
  };
}

void mousePressed() {
  for(Button b : buttons) {
    if(b.hitTest(mouseX, mouseY)) {
      switch(b.label) {
        case "New Game":
          state = 1;
          break;
      }
    }
  }
}

void draw() {
  background(0);
  for(Button b : buttons) {
    b.present();
  }
  text(state, 10, 10);
}
1 Like

I got it to work finally :slight_smile: Thank you so much!!!