Menu if-loop - a Mystery needs to be solved

Hey there fellow processors.

I am currently writing a little programm for our aesthetics course, which displays classical paintings and there titles. I am using two arrays for images and text. And the text only shows on right-click. This works pretty swell.

BUT now i wanted to make a menu. I figured, it should be possible to split the draw() in two if-loops. and when the mouse is pressed, a boolean changes and the if loop switches.

I did this, but it just never displays the menu (which should be a simple triangle on the screen). No errors.

Maybe someone finds the logical flaw in this?

Cheers (i shortened the code a bit, for better understanding``)

boolean clicked;

boolean menu;

void draw() {
    if (menu) {

    background(60, 49, 59);
    triangle(30, 75, 58, 20, 86, 75);

}
else {

    background(80, 69, 79);
    display = imgarray[number];
    image(display, width/2, height/2, display.width/3.5, display.height/3.5);
    surface.setSize(display.width/2, display.height/2);

    textSize(18);
    textAlign(CENTER, CENTER);
    if (clicked) {
    text(infos[number], width/2, height-((height/100)*13)); 
}
}
}

void mousePressed() {

    if (mouseButton == LEFT && menu == true) {
        menu = ! menu;
    }

    if (mouseButton == LEFT && menu == false) {

        number++;

    }

    if (mouseButton == RIGHT && menu == false) {

         clicked = ! clicked;

    }


}
Tagged:

Answers

  • edited June 2017 Answer ✓
    1. This code example doesn't run, with a pile of errors.
    2. Your mousePressed() logic is wrong in several places. For example, you initialize menu as false (default boolean value), and the only code to change it (menu = !menu) will only run if menu==true. So menu can never change.

    Start with a simple working example:

    boolean menu;
    
    void draw() {
      if (menu) {
        background(60, 49, 59);
        triangle(30, 75, 58, 20, 86, 75);
      } else {
        background(80, 69, 79);
      }
    }
    
    void mousePressed() {
      if (mouseButton == LEFT) {
        menu = ! menu;
      }
    }
    
  • Thanks Jeremy, that did the trick. I set the boolean menu to the wrong value.

    I shortened the code (to an expense where it won't run) because the definition of the array and so on takes up so much space - sorry, should have mentioned that.

  • Please say if-clause instead of if loop.

    A loop is something repetitive

Sign In or Register to comment.