How to set up a menu that disappears and goes to game when clicking with mouse?

edited November 2016 in Library Questions

Hi,

I'm trying to implement a menu into my simple game. All I am trying to do is to have this rectangle displayed with some text, and when the mouse is pressed, the rectangle disappears and the program goes to the game.

I tried a few different methods. First off, I suspended the loop using noLoop(), because having void loop going and doing all of the different things for the game disrupted my menu. So I thought I would suspend, and so an if statement like this: (menu_condition is initialized to true);

if (menu_condition)
{
 menu();//draw the menu
 noLoop();//pause the loop


 if(mousePressed)
    {
//if we pressed the mouse, than we resume the loop and we no longer enter into menu for the rest of the game
//because menu_condition is set to false
     menu_condition = false; 
     loop();
    }
}

However, the problem with this is that we are not looping, so even though we are clicking the mouse, the void loop is paused so the mousePressed is not being registered. I tried doing a while loop instead of "if", and that would freeze. I tried implementing this condition during the loop, but this caused a lot of problems, so it seems to me that keeping the loop static is one of the only ways of effectively doing this (I'm most likely wrong)...

The menu() function just draws a rect with some text...nothing else.

How can I successfully incorporate this menu?

Here is my full void and setup functions:

void setup ()
{ 
  minim = new Minim(this);
  cue_stick_hit = minim.loadSnippet("stick_cue_hit.wav");
  ball_wall_collision = minim.loadSnippet("ball_wall_collision.wav");
  size(900, 600);//size of processing display window
  frameRate(60); //Sets refresh rate of processing window
  cue.rad = 31.0;//diameter
  cue.col = #e2e9f0;//colour of cue ball


}

void draw ()
{ 

  table ();
  cue();
  stick();
  position.add(velocity_cue);
 checkBoundaryCollision();
 velocity_cue.mult(friction_factor);
 velocity_check();
 in_the_pocket();

if(boolean_condition == 1)
{
 noLoop();
}
 textSize(14);
 fill(text_color_r,text_color_g,text_color_b);
 text("POWER:"+power_percentage+"%", 100,100);

if (menu_condition)
{
 menu();
 noLoop();
 if(mousePressed)
    {
     menu_condition = false; 
     loop();
    }
}
}

Thank you!

Tagged:

Answers

  • Answer ✓

    Well i cannot run this code without the rest of it, but using "noLoop()" is not the best idea. Here is an example:

    boolean menuCondition = true;
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      if (menuCondition) {
        // "menu"
        background(255);
        fill(0);
        text("Click to start...", 20, 40);
      } else {
        // game
        fill(255);
        ellipse(mouseX, mouseY, 10, 10);
      }
    }
    
    void mousePressed() {
      background(0);
    
      // toggle menu on mouse-click
      menuCondition = !menuCondition;
    }
    

    The idea is that you decide in draw() if you want to show the menu or the game. the mousePressed() function is called whenever you click a mouse-button, and just toggles the "menuCondition"-variable in this case.

    If you have more than two states in your application, you could use a switch instead of the if/else that i used.

  • Quote:

    If you have more than two states in your application, you could use a switch instead of the if/else that i used.

    True. Then use an int state=0;

    You can assign values to it like final int stateMenu=0;

    final int stateGame = 1;

    final int stateHighScore = 2;

    etc.

  • Thank you! I really appreciate the help.

    Here is the code that made it work:

    void draw ()
    { 
      if(menu_condition)
      {
        table ();
        menu();
        cue();
        if(mousePressed)
        {
         menu_condition = false; 
        }
      }
    
      else
      { 
      table();
    
      stick();
      position.add(velocity_cue);
     checkBoundaryCollision();
     velocity_cue.mult(friction_factor);
     velocity_check();
     //in_the_pocket();
    
    if(boolean_condition == 1)
    {
     noLoop();
    }
     textSize(14);
     fill(text_color_r,text_color_g,text_color_b);
     text("POWER:"+power_percentage+"%", 100,100);
      }
    }
    

    I just called the functions that drew the graphics I wanted in the background, and left out the other functions that were messing up my menu because of the loop

  • Glad to hear that it worked, @MyName -- thank you for sharing your solution.

    To make your code even easier to read:

    In the Processing PDE app, highlight the code and press Command-T. This will fix all the indenting.

This discussion has been closed.