Error in changing screen

Hello, I am designing a Processing type game. I made it so I can click "buttons' (rectangle) and it draws something else; the part of the other screen. When I click the "Play" button, it goes immediately somewhere else, to the "Science" screen, which is not how it is supposed to work. When I check the variable of screen, which I am using to change drawings, it said it is 3, which means the I clicked twice, but I didn't. I would appreciate any help. Thanks!

``

int screen = 0;
void setup()  {
  size(1000,700);
}




void draw()  {
  background(1,0,175);
  if(screen == 0)  { //home screen
    //Title
    fill(255,255,255); //fill of rectangle of text box to white
    rectMode(CENTER); //center
    rect(width/2, height/10, 350,75); // centered and a little bit down from top
    textSize(30); //text size
    fill(0,0,0); //black color of text
    text("Jeopardy in Processing!",width/2-165, height/10+10); //centered in rectangle

    //Play button
    fill(255,255,255);
    rect(width/2,height/3+50,250,75); //below title button and smaller
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Play!",width/2-30,height/3+60); //makes the text inside the box
    //Instructions Button
    fill(255,255,255); //fill of rectangle of text box to white
    rect(width/2,height/2+50,250,75); //below play button 
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Instructions",width/2-83,height/2+60);
    //Credits button
    fill(255,255,255); //white color
    rect(width/2,height/2+163,250,75); //below instructions button
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Credits",width/2-50,height/2+173); //add the text
  }


  if (screen == 1)  { //play screen
    //Title "Choose Subject!"
    fill(255,255,255); //fill of rectangle of text box to white
    rectMode(CENTER); //center
    rect(width/2, height/10, 350,75); // centered and a little bit down from top
    textSize(30); //text size
    fill(0,0,0); //black color of text
    text("Choose Subject!",width/2-120, height/10+10); //centered in rectangle
    fill(255,255,255); //white rectangle
    rect(width/2,height/3+50,250,75); //below title button, Science
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Science!",width/2-55,height/3+60); //makes the "Science" text inside the box
    //Math
    fill(255,255,255); //fill of rectangle of text box to white
    rect(width/2,height/2+50,250,75); //below science button 
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Math",width/2-37,height/2+60);
    //Comp Sci button
    fill(255,255,255); //white color
    rect(width/2,height/2+163,250,75); //below instructions button
    textSize(30); //text size
    fill(0,0,0); //black text
    text("Comp Sci",width/2-65,height/2+173); //add the text

  }
  if (screen == 2)  {//instructions screen
   fill(255,255,255);
   rectMode(CENTER);
   rect(width/2,height/10,350,75); //rectangle
   //Text
   textSize(30); //text size
   fill(0,0,0); //black color of text
   text("Instructions",width/2-80,height/10+10); //centered in rectangle
   //Instruction box
   fill(255,255,255); //white rectangle
   rect(width/2,height/2-40,850,300);
   //Text in instruction box
   textSize(25); //text size
   fill(0,0,0);
   text("This is a Jeopardy game. You can work on teams or you can play by yourself. You can click the 'Play' button to get 3 subjects to study, Math, Science, Computer Science. You click the button for which subject you want to study. Then there will be topics for each subject, and you choose the cash amount, which is correlated to difficulty. You then answer the multiple choice questions, and get points based on that. Good Luck!" ,
   500,400,800,height/2+100);
   //Back button
   fill(255,255,255); //white rectangle
   rect(width/2,height/2+200,250,75); //rectangle below instructions
   fill(0,0,0); //black text
   text("Back",width/2-25,height/2+210); //text 
  }
  if (screen==3)  {//science screen
    text("science",width/2,height/2);


  }
}





void mousePressed()  {
  //Change the screen based on which button clicked
  //play button
  if (mouseX >= 375 && mouseX <= 625 && mouseY <= 320 && mouseY >= 244 && screen == 0)  { //if clicked play button
    println("Clicked play button"); //tell user
    screen = 1; //change screen variable 
    println("screen is",screen);
  }
  //instruction button
  if (mouseX >= 375 && mouseX <= 625 && mouseY <= 437 && mouseY >= 363 && screen == 0)  { //if clicked instructions button
    println("Clicked instructions button"); //tell user
    screen = 2; //change screen variable

  }
  //back button from instructions screen
  if (mouseX >= 375 && mouseX <= 624 && mouseY <=587 && mouseY >= 512 && screen == 2)  {
    screen = 0;


  }
  //Science button on play screen
  if (screen == 1)  { //if on the play screen to choose subjects
    if (mouseX >= 375 && mouseX <= 625 && mouseY <=325 && mouseY >= 243)  { //if click science button
      screen = 3; //go to science screen



    }
  }

  println(mouseX,mouseY);

}
Tagged:

Answers

  • edited May 2017 Answer ✓

    use also else if (screen == 1) { //play screen

    always else if in draw() except the first if

    same in mousePressed()- here also you need a good separation of the states of the program.

    Usage of switch()

    switch() might make your life even more easy:

    int screen = 0;
    
    
    void setup() {
      size(1000, 700);
    }
    
    
    
    
    void draw() {
      background(1, 0, 175);
    
      switch(screen) {
    
      case 0:  //home screen
        //Title
        fill(255, 255, 255); //fill of rectangle of text box to white
        rectMode(CENTER); //center
        rect(width/2, height/10, 350, 75); // centered and a little bit down from top
        textSize(30); //text size
        fill(0, 0, 0); //black color of text
        text("Jeopardy in Processing!", width/2-165, height/10+10); //centered in rectangle
    
        //Play button
        fill(255, 255, 255);
        rect(width/2, height/3+50, 250, 75); //below title button and smaller
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Play!", width/2-30, height/3+60); //makes the text inside the box
        //Instructions Button
        fill(255, 255, 255); //fill of rectangle of text box to white
        rect(width/2, height/2+50, 250, 75); //below play button 
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Instructions", width/2-83, height/2+60);
        //Credits button
        fill(255, 255, 255); //white color
        rect(width/2, height/2+163, 250, 75); //below instructions button
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Credits", width/2-50, height/2+173); //add the text
        break; 
    
    
      case 1: 
        //play screen
        //Title "Choose Subject!"
        fill(255, 255, 255); //fill of rectangle of text box to white
        rectMode(CENTER); //center
        rect(width/2, height/10, 350, 75); // centered and a little bit down from top
        textSize(30); //text size
        fill(0, 0, 0); //black color of text
        text("Choose Subject!", width/2-120, height/10+10); //centered in rectangle
        fill(255, 255, 255); //white rectangle
        rect(width/2, height/3+50, 250, 75); //below title button, Science
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Science!", width/2-55, height/3+60); //makes the "Science" text inside the box
        //Math
        fill(255, 255, 255); //fill of rectangle of text box to white
        rect(width/2, height/2+50, 250, 75); //below science button 
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Math", width/2-37, height/2+60);
        //Comp Sci button
        fill(255, 255, 255); //white color
        rect(width/2, height/2+163, 250, 75); //below instructions button
        textSize(30); //text size
        fill(0, 0, 0); //black text
        text("Comp Sci", width/2-65, height/2+173); //add the text
        break;
    
    
      case 2: 
        //instructions screen
        fill(255, 255, 255);
        rectMode(CENTER);
        rect(width/2, height/10, 350, 75); //rectangle
        //Text
        textSize(30); //text size
        fill(0, 0, 0); //black color of text
        text("Instructions", width/2-80, height/10+10); //centered in rectangle
        //Instruction box
        fill(255, 255, 255); //white rectangle
        rect(width/2, height/2-40, 850, 300);
    
        //Text in instruction box
        textSize(25); //text size
        fill(0, 0, 0);
        text("This is a Jeopardy game. You can work on teams or you can play by yourself. "
          +"You can click the 'Play' button to get 3 subjects to study, Math, Science, Computer Science. "
          +"You click the button for which subject you want to study. "
          +"Then there will be topics for each subject, and you choose the cash amount, which is correlated to difficulty. "
          +"You then answer the multiple choice questions, and get points based on that. Good Luck!", 
          500, 400, 800, height/2+100);
        //Back button
        fill(255, 255, 255); //white rectangle
        rect(width/2, height/2+200, 250, 75); //rectangle below instructions
        fill(0, 0, 0); //black text
        text("Back", width/2-25, height/2+210); //text
        break;
    
      case 3:    //science screen
        text("science", width/2, height/2);
        break;
      }//switch
    }
    
    //------------------------------------------------------------
    
    void mousePressed() {
      //Change the screen based on which button clicked
    
      switch(screen) {
    
      case 0:  //home screen
    
        //play button
        if (mouseX >= 375 && mouseX <= 625 && mouseY <= 320 && mouseY >= 244 ) {
          //if clicked play button
          println("Clicked play button"); //tell user
          screen = 1; //change screen variable 
          println("screen is", screen);
        }
        //instruction button
        else  if (mouseX >= 375 && mouseX <= 625 && mouseY <= 437 && mouseY >= 363 ) { 
          //if clicked instructions button
          println("Clicked instructions button"); //tell user
          screen = 2; //change screen variable
        }
        break;
    
        //Science button on play screen
      case 1:
        //if on the play screen to choose subjects
        if (mouseX >= 375 && mouseX <= 625 && mouseY <=325 && mouseY >= 243) { 
          //if click science button
          screen = 3; //go to science screen
        }
        break; 
    
      case 2: 
        //back button from instructions screen
        if (mouseX >= 375 && mouseX <= 624 && mouseY <=587 && mouseY >= 512 ) {
          screen = 0;
        }
        break;
      }//switch 
    
      println(mouseX, mouseY);
    }
    //
    
  • Thanks for the help on the else if, it worked!

Sign In or Register to comment.