How to change between stages Processing

edited April 2017 in Questions about Code

Hello!

I am making a "game box". I have a simple main menu where i want to access my 2 processing games from. I am using keys for this, so if(keyPressed) { if (key =='1') { stage = 1; if '2' is pressed then stage = 2, same with stage 3. Problem is that when i press '2', i move to stage 2, but when i press 1 or 3, nothing happens. I've tried printing the stage to the console and it writes 1 when i start it up, but when i go to either stage 2 or 3 prints once and then it stops.

Here's a bit (starts in stage 1 defined in setup): if(keyPressed) { if (key == '1') { stage = 1; } if (key == '2') { stage = 2; } if (key == '3') { stage = 3; } }

if (stage == 2){ background(255,0,0); //red Game1 code is supposed to go here println(stage);

if(keyPressed) { if (key == '1') { stage = 1; } if (key == '2') { stage = 2; } if (key == '3') { stage = 3; } } } if (stage == 3){ background(0,255,0); //green Game2 code is supposed to go here println(stage);

if(keyPressed) { if (key == '1') { stage = 1; } if (key == '2') { stage = 2; } if (key == '3') { stage = 3; } } }

Tagged:

Answers

  • I have now pasted the game into one of the stages and have encountered another problem. I'm using a void mousePressed and i get a "unexpected token:void error.

  • several issues here.

    First: if(keyPressed takes ONE key press 3 or 4 times

    instead of using if(keyPressed... better use the function of the same name:

    second use if...else if ... instead of if...if...if... (also in draw() !!!)

        void keyPressed() {
    
            if (key == '1') { 
              stage = 1;
            } 
            else if (key == '2') { 
              stage = 2;
            } 
            else if (key == '3') { 
              stage = 3;
            } 
    
        }
    

    Third post runnable code or a mcve:

    https://stackoverflow.com/help/mcve

  • Thank you for your response Chrisir! I figured out the if.. else if thing before i saw your reply. In response to the first issue you addressed, if i copy the part you wrote and use void keyPressed instead of if (keyPressed) i get the "unexpeced token: void" error.

  • ... Which is impossible for us to debug without seeing the runnable code or an MCVE. Which is why you should post runnable code or an MVCE.

  • Of course, here you go. I've made it sort of "working" by replacing my voids with if's. This obviously causes an issue which you will see if you look at it. https://pastebin.com/u3Hsnsji

  • The VERY FIRST thing to fix is to move any line that has "loadImage" in it into setup()! You do NOT want to be reloading your images 60 times a second, right? That's what you're doing by having those calls in draw()!

    Next, understand the difference between "void keyPressed()" and "keyPressed". The former is a top-level function, like draw() or setup(), that is called once when a key is pressed. The latter is a boolean variable that is true if a key is being held down. The function is what Chrisir is suggesting you use!

    Now I can't test this without your images, but I shuffled things about a bit, and this is the result:

    PImage startscreen, backImg, birdImg, wallImg, startImg;
    PFont title;
    int stage;
    int screensizex = 1728;
    int screensizey = 972;
    int gamestate = 1, score = 0, highScore = 0, x = -200, y, vy = 0, wx[] = new int[2], wy[] = new int[2];
    
    
    void setup() {
      size(1728, 972);
      stage = 1;
      startscreen = loadImage("Tesla.jpg");
      backImg =loadImage("background.png");
      birdImg =loadImage("bird.png");
      wallImg =loadImage("wall.png");
      startImg=loadImage("menu.png");
    }
    
    void draw() {
      if (stage==1) {
        image(startscreen, 0, 0, screensizex, screensizey);
        title = createFont("font", 1000, true);
        textAlign(CENTER);
        text("BLA BLA!", 100, 150);
        text("Test text", 100, 170);
        println(stage);
      }
      else if (stage == 2) {
        println(stage);
        fill(0);
        if (gamestate == 0) {
          imageMode(CORNER);
          image(backImg, x, 0);
          image(backImg, x+backImg.width, 0);
          x -= 6;
          vy += 1;
          y += vy;
          if (x == -1800) x = 0;
          for (int i = 0; i < 2; i++) {
            imageMode(CENTER);
            image(wallImg, wx[i], wy[i] - (wallImg.height/2+100));
            image(wallImg, wx[i], wy[i] + (wallImg.height/2+100));
            if (wx[i] < 0) {
              wy[i] = (int)random(200, height-200);
              wx[i] = width;
            }
            if (wx[i] == width/2) highScore = max(++score, highScore);
            if (y>height||y
              <0||(abs(width/2-wx[i])
              <25 && abs(y-wy[i])>100)) gamestate=1;
            wx[i] -= 6;
          }
          image(birdImg, width/2, y);
          text(""+score, width/2-15, 700);
        } else {
          imageMode(CENTER);
          image(startImg, width/2, height/2);
          text("High Score: "+highScore, 50, width);
        }
      } else if (stage == 3) {
        background(0, 255, 0); //green
        println(stage);
      }
    }
    
    void mousePressed() {
      vy = -17;
      if (gamestate==1) {
        wx[0] = 600;
        wy[0] = y = height/2;
        wx[1] = 900;
        wy[1] = 600;
        x = gamestate = score = 0;
      }
    }
    
    void keyPressed() {
      if (key == '1') {
        stage = 1;
      } else if (key == '2') {
        stage = 2;
        gamestate = 0; // ???
      } else if (key == '3') {
        stage = 3;
      }
    }
    
Sign In or Register to comment.