Switching between scenes with buttons

Hello,

I am working on a point and click game and would like to switch between scenes with some buttons. I already made all the scenes which are in separate classes as well as the button class. The button class contains two methods one for setting the button and the other for getting the button. Each of the scenes contains two methods as well, one for loading the background image and the other for loading all the functionality of the scene like buttons, spots and signs.

class Button {
    Button() { }
        void setButton( float plX, float pmX, float plY, float pmY, float iX, float iY) {
            this.poslX= plX;
            this.posmX = pmX;
            this.poslY = plY;
            this.posmY = pmY;
            this.imgX = iX;
            this.imgY = iY;
          }

           boolean getButton() {
              if(mouseX > plX && mouseX < pmX && mouseY > plY && mouseY < pmY) {
                image(img, pX, pY);
                if(mousePressed) {
                  click = true;
                }
              }
              else {
                image(imgH, pX, pY);
                clicked = false;
              }
              return clicked;
          }
    }

I call all the buttons like this in a method called mainScene()

class SceneOne {
       SceneOne() { // some images go here }

void mainScene() {
       button.setClickableArea(1125, 1125 + 164, 60, 60 + 165, 1125, 60);
       button.getClickableArea();

       button.setClickableArea(1125, 1125 + 164, 500, 500 + 165, 1125, 500);
       button.getClickableArea();
      }
}

Also i have a Stages class which should contain all the scenes as well as the switching logic. I was planning only to put one method in draw() called gameStart(). The question is how do i make the buttons switch to a particular scene?

Thanks

Answers

  • Answer ✓

    You need to have states, number indicating which stage you are on.

    In draw(), use switch () to call the proper scene, depending on the state.

    On button click, change this state, eg. by incrementing it.

  • I already solved it that way :). I remembered that there was something like that in this forum. Thanks.

  • edited March 2017

    You can switch the scenes by creating a simple SceneManager. It will allow you to switch to any scene whenever you want (when you click on a button or when a particular scene ends).

    This is a p5.js implementation of a SceneManager: https://github.com/mveteanu/p5.SceneManager

Sign In or Register to comment.