We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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!
Answers
Well i cannot run this code without the rest of it, but using "noLoop()" is not the best idea. Here is an example:
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:
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.
now here:
https://forum.processing.org/two/discussion/19555/how-to-incorporate-a-countdown-into-game-help