How to alternate between three states

edited June 2016 in How To...

Hi, I am making a game in Processing. One part is the home screen, a second part is the actual game, and the third part is going to be a game over screen. I can make it so the home screen goes to the actual game by using a boolean, but how would I display the game over screen, with everything else not shown?

Tagged:

Answers

  • Answer ✓

    Stop using a boolean. Instead, number your states 0 (for the home page), 1 (the game), and 2 (game over screen). Then use an integer to remember which state you're in instead of a boolean. Change you if statements to check the value of that integer instead of just checking if your boolean is true or false.

    int state;
    
    if( state == 0){
    
    } else if(state ==1){
    
    } else if(state == 2) {
    
    }
    
  • I find using numbers a bit cryptic so usually define constants at the top of the code for each state and then use those names throughout:

    static final int HOME_PAGE = 0;
    ...
    
    if( state == HOME_PAGE){
    ...
    
  • edited June 2016 Answer ✓
    void draw() {
    
        // outside switch nothing is allowed in draw() !!!
    
        switch(state) {
    
            case HOME_PAGE:
            //
            displayHomepage();
            break;
    
            case GAME_PAGE:
            //
            displayGame();
            break;
    
            case GAME_OVER_PAGE:
            //
            displayGameOver();
            break;
    
        }//switch
    
    }//draw()
    
    // -------------------
    
    void displayHomepage() {
    
    }
    
    
    void displayGame() {
    
    }
    
    
    
    void displayGameOver() {
    
    }
    
Sign In or Register to comment.