I have several issues with my else if and if structures

My mousePressed if else structure seems to be skipping some “estado” and not working at all

int estado = 0;

// Establezco las imagenes que van a usarse.

PImage img0, img1, img2, img3, img4, img5, img6;

// Establezco una manera de llamar a mi fuente personalizada

PFont miFuente;

// Establezco los valores en x e y para los botones

float boton_inicio_x=300, boton_inicio_y=450, radio_boton_i=50;

float boton_izquierdo_x=100, boton_izquierdo_y=520, radio_boton_l=50;

float boton_derecho_x=500, boton_derecho_y=520, radio_boton_r=50;

// Establezco las variables para los creditos

float posY;

void setup(){
  size(600,600);
  
  posY= random(10);
  
  // Cargo las imagenes que estableci
  
  img0 = loadImage("inicio.jpg");
  img1 = loadImage("puertas.jpg");
  img2= loadImage("banio.jpg");
  img3 = loadImage("creditos.jpg");
  img4 = loadImage("comedor.jpg");
  img5 = loadImage ("cocina.jpg");
  img6 = loadImage ("ventilacion.jpg");
  
  // Cargo la fuente 
  
  miFuente = loadFont("Candara-BoldItalic-48.vlw");
  
  textFont ( miFuente );
}

void draw(){
  background(255);
  if (estado == 0){
    image(img0,0,0,width,height);
    textAlign(CENTER, CENTER);
    text("Vida de un raton", 300,100);
    textSize(28);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    text("Iniciar", boton_inicio_x, boton_inicio_y);
  } else if (estado == 1) {
    image(img1,0,0,width,height);
    
    // Dibujo el recuadro donde van los botones
    
    pushStyle();
    fill(86, 43, 5);
    rect(0, 450, 600,300);
    popStyle();
    
    // Texto de la pantalla 1
    pushStyle();
    textSize(15);
    text("Eres deseak, un raton que se encuentra escapando de la casa de un duque", width/2, 50);
    text("Te encuentras con dos puertas, ¿Cual eliges?", width/2, 75);
    popStyle();
    // Texto opciones
    
    // Dibujo los botones
    pushStyle();
    fill(#6d461c);
    ellipse( boton_izquierdo_x,boton_izquierdo_y,radio_boton_l*2,radio_boton_l*2);
    ellipse(boton_derecho_x, boton_derecho_y,radio_boton_r*2,radio_boton_r*2); 
    popStyle();
    pushStyle();
    textSize(20);
    text("Izquierda", boton_izquierdo_x, boton_izquierdo_y);
    text("Derecha", boton_derecho_x, boton_derecho_y);
    popStyle();
  } else if (estado == 2){
    image(img2,0,0,width,height);
    
    // Texto de la pantalla 2
    
    pushStyle();
    textSize(15);
    text("La puerta izquierda llevaba al baño", width/2, 50);
    text("Intentas esconderte pero te terminan atrapando", width/2, 75);
    popStyle();
    
    textSize(15);
    text("FINAL MALO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
    
  }
    else if (estado ==3){
      image(img3,0,0,width,height);
            // Texto de creditos
      pushStyle();
      textSize(20);
       text("Nicolas", width/2, posY );
       text("Informatica", width/2, posY+20);
      popStyle();
       posY-=-0.2;
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
       text("Inicio", boton_inicio_x, boton_inicio_y);
    popStyle();
      
    }
   else if ( estado ==4){
     image(img4,0,0,width,height);
     
    // Dibujo el recuadro donde van los botones
    
    pushStyle();
    fill(86, 43, 5);
    rect(0, 450, 600,300);
    popStyle();
    
     // Texto de la pantalla 4
    pushStyle();
    textSize(15);
    text("Llegas al comedor, ves una puerta y una ventilacion", width/2, 50);
    text("¿Que camino eliges?", width/2, 75);
    popStyle();
     // Dibujo los botones
    pushStyle();
    fill(#6d461c);
    ellipse( boton_izquierdo_x,boton_izquierdo_y,radio_boton_l*2,radio_boton_l*2);
    ellipse(boton_derecho_x, boton_derecho_y,radio_boton_r*2,radio_boton_r*2); 
    popStyle();
    pushStyle();
    textSize(20);
    text("Cocina", boton_izquierdo_x, boton_izquierdo_y);
    text("Ventilacion", boton_derecho_x, boton_derecho_y);
    popStyle();
    
   }
   else if (estado ==5){
     image(img5,0,0,width,height);
    
    // Texto de la pantalla 5
    
    pushStyle();
    textSize(15);
    text("Llegas a la cocina, puedes escuchar al duque llegar", width/2, 50);
    text("Intentas esconderte pero te terminan atrapando", width/2, 75);
    popStyle();
    
    textSize(15);
    text("FINAL MALO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
      
} else if (estado==6){
  image(img6,0,0,width,height);
  // Texto de la pantalla 6
    pushStyle();
    textSize(15);
    text("Llegas a un conducto de ventilacion", width/2, 50);
    text("Escapas sano y salvo", width/2, 75);
    popStyle();
    
    textSize(15);
    text("FINAL BUENO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
}
  
  
}

void mousePressed(){
  if (estado ==0){
    estado =1;}
  
   else if ( estado == 1 ) {
      if (dist(mouseX,mouseY, boton_izquierdo_x, boton_izquierdo_y)<radio_boton_l){
        estado = 2;
      } 
      if (dist(mouseX,mouseY, boton_derecho_x, boton_derecho_y)<radio_boton_r) {
        estado = 4;
}
} else if (estado ==2) {
  if(dist(mouseX,mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i){
    estado = 3;
  } else if (estado ==3){
  if(dist(mouseX,mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i) {
    estado = 0;  
} else if (estado == 4) {
      if (dist(mouseX,mouseY, boton_izquierdo_x, boton_izquierdo_y)<radio_boton_l){
        estado = 5;
      } 
      if (dist(mouseX,mouseY, boton_derecho_x, boton_derecho_y)<radio_boton_r) {
        estado = 6;
  } else if (estado == 5) {
    if(dist(mouseX,mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i){
    estado = 3;
}
}
}
}
}
}

in processing hit ctrl-t to get auto-indents.

From the indents in mousePressed() you will see your errors.

The indents of the brackets { } are not where you expect them.

After else if (estado ==2) {



int estado = 0;

// Establezco las imagenes que van a usarse.
PImage img0, img1, img2, img3, img4, img5, img6;

// Establezco una manera de llamar a mi fuente personalizada
PFont miFuente;

// Establezco los valores en x e y para los botones
float boton_inicio_x=300, boton_inicio_y=450, radio_boton_i=50;
float boton_izquierdo_x=100, boton_izquierdo_y=520, radio_boton_l=50;
float boton_derecho_x=500, boton_derecho_y=520, radio_boton_r=50;

// Establezco las variables para los creditos
float posY;

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

  posY= random(10);

  // Cargo las imagenes que estableci
  img0 = loadImage("inicio.jpg");
  img1 = loadImage("puertas.jpg");
  img2= loadImage("banio.jpg");
  img3 = loadImage("creditos.jpg");
  img4 = loadImage("comedor.jpg");
  img5 = loadImage ("cocina.jpg");
  img6 = loadImage ("ventilacion.jpg");

  // Cargo la fuente 
  miFuente = loadFont("Candara-BoldItalic-48.vlw");
  textFont ( miFuente );
}

void draw() {
  background(255);
  if (estado == 0) {
    image(img0, 0, 0, width, height);
    textAlign(CENTER, CENTER);
    text("Vida de un raton", 300, 100);
    textSize(28);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    text("Iniciar", boton_inicio_x, boton_inicio_y);
  } else if (estado == 1) {
    image(img1, 0, 0, width, height);

    // Dibujo el recuadro donde van los botones

    pushStyle();
    fill(86, 43, 5);
    rect(0, 450, 600, 300);
    popStyle();

    // Texto de la pantalla 1
    pushStyle();
    textSize(15);
    text("Eres deseak, un raton que se encuentra escapando de la casa de un duque", width/2, 50);
    text("Te encuentras con dos puertas, ¿Cual eliges?", width/2, 75);
    popStyle();
    // Texto opciones

    // Dibujo los botones
    pushStyle();
    fill(#6d461c);
    ellipse( boton_izquierdo_x, boton_izquierdo_y, radio_boton_l*2, radio_boton_l*2);
    ellipse(boton_derecho_x, boton_derecho_y, radio_boton_r*2, radio_boton_r*2); 
    popStyle();
    pushStyle();
    textSize(20);
    text("Izquierda", boton_izquierdo_x, boton_izquierdo_y);
    text("Derecha", boton_derecho_x, boton_derecho_y);
    popStyle();
  } else if (estado == 2) {
    image(img2, 0, 0, width, height);

    // Texto de la pantalla 2

    pushStyle();
    textSize(15);
    text("La puerta izquierda llevaba al baño", width/2, 50);
    text("Intentas esconderte pero te terminan atrapando", width/2, 75);
    popStyle();

    textSize(15);
    text("FINAL MALO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
  } else if (estado ==3) {
    image(img3, 0, 0, width, height);
    // Texto de creditos
    pushStyle();
    textSize(20);
    text("Nicolas", width/2, posY );
    text("Informatica", width/2, posY+20);
    popStyle();
    posY-=-0.2;
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Inicio", boton_inicio_x, boton_inicio_y);
    popStyle();
  } else if ( estado ==4) {
    image(img4, 0, 0, width, height);

    // Dibujo el recuadro donde van los botones

    pushStyle();
    fill(86, 43, 5);
    rect(0, 450, 600, 300);
    popStyle();

    // Texto de la pantalla 4
    pushStyle();
    textSize(15);
    text("Llegas al comedor, ves una puerta y una ventilacion", width/2, 50);
    text("¿Que camino eliges?", width/2, 75);
    popStyle();
    // Dibujo los botones
    pushStyle();
    fill(#6d461c);
    ellipse( boton_izquierdo_x, boton_izquierdo_y, radio_boton_l*2, radio_boton_l*2);
    ellipse(boton_derecho_x, boton_derecho_y, radio_boton_r*2, radio_boton_r*2); 
    popStyle();
    pushStyle();
    textSize(20);
    text("Cocina", boton_izquierdo_x, boton_izquierdo_y);
    text("Ventilacion", boton_derecho_x, boton_derecho_y);
    popStyle();
  } else if (estado ==5) {
    image(img5, 0, 0, width, height);

    // Texto de la pantalla 5

    pushStyle();
    textSize(15);
    text("Llegas a la cocina, puedes escuchar al duque llegar", width/2, 50);
    text("Intentas esconderte pero te terminan atrapando", width/2, 75);
    popStyle();

    textSize(15);
    text("FINAL MALO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
  } else if (estado==6) {
    image(img6, 0, 0, width, height);
    // Texto de la pantalla 6
    pushStyle();
    textSize(15);
    text("Llegas a un conducto de ventilacion", width/2, 50);
    text("Escapas sano y salvo", width/2, 75);
    popStyle();

    textSize(15);
    text("FINAL BUENO", width/2, 550);
    pushStyle();
    fill(86, 43, 5);
    ellipse(boton_inicio_x, boton_inicio_y, radio_boton_i*2, radio_boton_i*2);
    popStyle();
    pushStyle();
    textSize(20);
    text("Creditos", boton_inicio_x, boton_inicio_y);
    popStyle();
  }
}//func 

void mousePressed() {
  if (estado ==0) {
    estado =1;
  } else if ( estado == 1 ) {
    if (dist(mouseX, mouseY, boton_izquierdo_x, boton_izquierdo_y)<radio_boton_l) {
      estado = 2;
    } 
    if (dist(mouseX, mouseY, boton_derecho_x, boton_derecho_y)<radio_boton_r) {
      estado = 4;
    }
  } else if (estado ==2) {
    if (dist(mouseX, mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i) {
      estado = 3;
    } else if (estado ==3) {
      if (dist(mouseX, mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i) {
        estado = 0;
      } else if (estado == 4) {
        if (dist(mouseX, mouseY, boton_izquierdo_x, boton_izquierdo_y)<radio_boton_l) {
          estado = 5;
        } 
        if (dist(mouseX, mouseY, boton_derecho_x, boton_derecho_y)<radio_boton_r) {
          estado = 6;
        } else if (estado == 5) {
          if (dist(mouseX, mouseY, boton_inicio_x, boton_inicio_y)<radio_boton_i) {
            estado = 3;
          }
        }
      }
    }
  }
}//func 
//

1 Like

Hi