Code will not switch between menus

edited January 2016 in Questions about Code

Hello everyone! I am having some issues with my start up menu for my game. I want it to switch from the main menu to the directions menu then to the game. It will switch from the main menu to the game but it wont execute the switch from main menu to directions menu. here is my code. Please help.

int stage;

void setup() {

stage = 1;

size(800,500);

nightsky = new NightSky( 100 );

Title_S = loadImage("Title_Screen.png");

Direct = loadImage("Directions.png");

Back_G = loadImage("Background.png");

Fore_G = loadImage("Ground.png");

image(Title_S, 0,0,width,height);

frameRate(5);

smooth();

}

void draw() {

if(stage==1){

if(keyPressed == true){

stage=2; } }

if(stage==2){

if(keyPressed == true){

 image(Direct,0,0,width,height);

 stage=3;

} }

if(stage==3){

background(0);

nightsky.draw();

image(Back_G,0,0);

image(Fore_G,0,0);

} }

Tagged:

Answers

  • ok so it appeared fine when i pasted it but when i posted it in discussion the coding got smushed together.

  • That didn't help me at all.

  • go back edit your post

    leave 2 empty lines before and after the code

    select the entire code without the 4 lines with the mouse

    hit ctrl-o

  • yea i fixed it but i still need help with my code please. right now it does this....if i press any key it switches the second image. i want it to switch the the 3rd image when a key is pressed on the second one too. it will only switch between 2 images not 3.

  • Answer ✓

    here...

    reason is just keyPressed as a boolean var (as you have it) registers immediately.

    Since you use if...if...if... the 2nd and 3rd registers immediately - solution: if ... if else... if else... then only one block would be executed.

    Moreover: keyPressed as a boolean var can register one key multiple types. Bad. Solution: Instead of var use a function keyPressed() - this registers only one key at a type. Use stage here as well...

    ;-)

    int stage;
    
    void setup() {
    
      stage = 1;
    
      size(800, 500);
    
      nightsky = new NightSky( 100 );
    
      Title_S = loadImage("Title_Screen.png");
    
      Direct = loadImage("Directions.png");
    
      Back_G = loadImage("Background.png");
    
      Fore_G = loadImage("Ground.png");
    
      image(Title_S, 0, 0, width, height);
    
      frameRate(5);
    
      smooth();
    }
    
    void draw() {
      if (stage==1) {
        //
      }
      else if (stage==2) {
        if (keyPressed == true) {
          stage=3;
        }
      }
      else if (stage==3) {
        background(0);
        nightsky.draw();
        image(Back_G, 0, 0);
        image(Fore_G, 0, 0);
      }
    } // func 
    
    // -------------------------------
    
    void keyPressed() {
      if (stage==1) {
        stage=2;
      }
      else if (stage==2) {
        image(Direct, 0, 0, width, height);
        stage=3;
      } 
      else if (stage==3) {
        // do nothing
      }
    } // func 
    // 
    
  • Thank you so much! youre a life saver!

  • Okay, it worked fine when i put it in but now its only running nightsky.draw() when a key is pressed but i want it to run all the time behind the 3rd image. how do i fix that?

  • show your code please

  • nightsky is a starfield i put behind image(Back_G, 0, 0); and image(Fore_G, 0, 0);

    void keyPressed() {

    if (stage==1) {

    stage=2;
    

    }

    else if (stage==2) {

    image(Direct, 0, 0, width, height);
    
    stage=3;
    

    }

    else if (stage==3) {

    background(0);
    
    nightsky.draw();
    
    image(Back_G, 0, 0);
    
    image(Fore_G, 0, 0);
    

    }

  • go back edit your post

    leave 2 empty lines before and after the code

    select the entire code without the 4 lines with the mouse

    hit ctrl-o

  • ??

    you must handle the key stuff in function keyPressed and showing the images in draw()

  • I don't understand.

  • void draw() {
      if (stage==1) {
        //
      }
      else if (stage==2) {
         //
      }
      else if (stage==3) {
        background(0);
        nightsky.draw();
        image(Back_G, 0, 0);
        image(Fore_G, 0, 0);
      }
    } // func
    
    // -------------------------------
    
    void keyPressed() {
      if (stage==1) {
        stage=2;
      }
      else if (stage==2) {
        image(Direct, 0, 0, width, height);
        stage=3;
      }
      else if (stage==3) {
        // do nothing
      }
    } // func 
    
Sign In or Register to comment.