Beginner: new pages (state)

HI, I am trying to figure out how to create a number of pages in my sketch. I don’t want to just reset the background. I know that you may need to use the switch case function, but I don’t understand how it works. Can someone please explain or show me a basic example.

1 Like

The core idea is that you have a variable named state or screen that indicates on which page you are.

here is an example.

Chrisir


//
// states
final int stateGame  = 0; // consts
final int statePause = 1;
int state = stateGame;    // current 

// -------------------------------------------------

void setup() {
  // init (runs only once)
  size(800, 600);
} // func 

void draw() { 
  // runs on and on 

  switch (state) {

  case stateGame: 
    // Game
    handleStateGame();
    break; 

  case statePause:
    // Pause
    handleStatePause(); 
    break;

  default:
    // error
    println("Error number 939; unknown state : " 
      + state
      + ".");
    exit();
    break;
  } // switch
  //
} // func 

// functions for states - called from draw() 

void handleStateGame() {
  // Game
  background(11);
  fill(244, 3, 3); // red 
  text ("This is the Game....\n Don't touch the red rectangle...", 210, 313);

  //
  noStroke();
  fill(255, 2, 2) ;
  rect(100, 100, 100, 100);
  fill(255, 2, 255) ;
  // rect(300, 100, 100, 100);

  if (mouseX > 100 && mouseX < 100 + 100  &&
    mouseY > 100   && mouseY < 100 + 100) {
    println ("collide");
    state = statePause;
  }
} // func 

void handleStatePause() {
  // Pause
  background(255);
  fill(244, 3, 3); // red 
  text ("You LOST....  " 
    + "   ...   ", 210, 313);

  text ("Hit p to start Game", 210, 385);
} // func 

// ----------------------------------------
// keyboard input 

void keyPressed() {

  switch (state) {

  case stateGame: 
    // Game
    keyPressedForStateGame();
    break; 

  case statePause:
    // Pause
    keyPressedForStatePause(); 
    break;

  default:
    // error
    println("Error number 1039; unknown state : " 
      + state 
      + ".");
    exit();
    break;
  } // switch
} // func 

// ----

void keyPressedForStateGame() { 
  if (key == CODED) 
  {
    if (keyCode == UP) {
      //
    } else if (keyCode == DOWN) {
      //
    }

    if (keyCode == LEFT) {
      //
    } else if (keyCode == RIGHT) {
      //
    } else {
      // do nothing
    } // else
  } //  if (key == CODED) {
  else 
  {
    // not CODED ---------------------- 
    if (key == 'p') {
      // Pause 
      state = statePause;
    } else {
      // do nothing
    } // else
  } // else not CODED
} // func

void keyPressedForStatePause() { 
  if (key == CODED) 
  {
    if (keyCode == UP) {
      //
    } else if (keyCode == DOWN) {
      // none
    }

    if (keyCode == LEFT) {
      //
    } else if (keyCode == RIGHT) {
      //
    } else {
      // do nothing
    } // else
  } //  if (key == CODED) {
  else 
  {
    // not CODED ---------------------- 
    if (key == 'p') {
      //
      state = stateGame;
    } else {
      // do nothing
    } // else
  } // else not CODED
} // func

// -------------------------------------------------------
1 Like

This is quite helpful. Does the keypressed funtions do anything in this code (except the p button). Also would I always need to include a default state?

1 Like

I included this to make sure that you use switch(state) in keypressed as well. And in mousePressed.

technically no. It’s useful sometimes to get an explicit error though.

2 Likes