0

I have loaded and created two image buttons menuStart and menuExit withe menuStart.png and menuExit.png:

image(menuStart, 250, 350, 100, 42);

image(menuExit, 450, 345, 110, 45);

What I've doing: I have set my pages as stages. stage 1 is a menu, stage 2 is a start screen stage 3 is a select difficulty screen + game, stage 4 is an exit screen, and stage 5 exits a game. I want to use the function mousePressed so that if the user selects the menuStart button in stage 1, stage = 2. Similarly if the user selects the menuExit button in stage 1, stage = 5.

What I've done with code: I have implemented the mousePressed but do not know how to set parameters for mousePressed in an image parameter. How do I go about setting this up?

code:

void doMenu() {
  // Stage 1 Start -- MENU
  if (stage == 1) {
    textFont(title);
    text("Game", 150, 200);
    textFont(subtitle);
    image(menuStart, 250, 350, 100, 42);
    image(menuExit, 450, 345, 110, 45);
    mousePressed();

    if(mousePressed == true) {
      stage = 2;
    }
  }
  // Stage 2 START
  if (stage == 2) {
    background(255);
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500);

    if(mousePressed == true) { // true -->start
      stage = 3; // go-to stage 3
    }
    /* else if(mousePressed == exit && stage != 2 { // exit
      stage = 5; // go-to exit
    }
    */
  }
  if(stage == 3) {
    background(255);
    startScreen = loadImage("start.png"); 
    image(startScreen, 0, 0, 800, 500);
    text("Press N for Normal or H for Hard", 200, 375);

    if(mousePressed == true) { // true --> hard
      hard = true;
      normal = false;
      startMenu = false;
    }
    /*
    else if(mousePressed == normal) { // normal
      hard = false;
      normal = true;
      startMenu = false;
    }
    */
    /*
    if(mousePressed == true) { // easy
      hard = false;
      normal = true;
      startMenu = false;
    }
    */
  }
  // Stage 4 EXIT
  if (stage == 4) { 
  background(0);
  exitScreen = loadImage("exit.jpg"); 
  image(exitScreen, 0, 0, 800, 400);
  textFont(subtitle);
  text("Press X to Exit", 300, 375);
  if(mousePressed == true) {
      stage = 5;
    }
  }
  if(stage == 5) {
    exit();
  }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
user2990
  • 15
  • 1
  • 8

1 Answers1

0

The mousePressed variable does not work the way you're describing. It is simply a boolean variable that holds true when the mouse is being pressed and false when it's not. It does not contain any information about what's being clicked.

To check what's being clicked, you're going to have to use the mouseX and mouseY variables and if statements to check whether the cursor is inside the button. A simple example looks like this:

if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){
   //mouse is inside button
}

If the mouse is being pressed and the mouse is inside the button, then the button is bring pressed. You can use nested if statements to check this, or you could use the mousePressed() function:

void mousePressed(){
    if(mouseX > buttonX && mouseX < buttonX + buttonWidth && mouseY > buttonY && mouseY < buttonY + buttonHeight){
       //mouse is clicking button
    }
}

Shameless self-promotion: here is a tutorial on getting use input in Processing.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107