Code within loop still triggering even though condition isn't true?
in
Programming Questions
•
3 years ago
Hi,
I'm new to processing and we've been instructed to build a small basic computer game. My role is creating the menu system and integrating the parts together.
However the problem I've got so far is that certain areas of code seem to be active, even though they are in an if statement based on conditions that aren't true.
When clicking on the 'about' section, I've instructed it to change from state 3 back to state 0 (the menu). However, when clicking on parts of the screen, the button functions from the menu (state 0) seem to be active. ie. If you click where the 'play' button is on the menu, but on the about section it still goes to the play section (state 1) of the game.
Yet, when you go to other sections like 'High Scores' and 'Play' (states 1 and 2), you don't get the same problem. Even though the if statement means the exact same thing.
It's as though the code from the menu (state 0) is still being called, even though the state has been set to something else.
Can anyone help me with this? Thanks.
I'm new to processing and we've been instructed to build a small basic computer game. My role is creating the menu system and integrating the parts together.
However the problem I've got so far is that certain areas of code seem to be active, even though they are in an if statement based on conditions that aren't true.
When clicking on the 'about' section, I've instructed it to change from state 3 back to state 0 (the menu). However, when clicking on parts of the screen, the button functions from the menu (state 0) seem to be active. ie. If you click where the 'play' button is on the menu, but on the about section it still goes to the play section (state 1) of the game.
Yet, when you go to other sections like 'High Scores' and 'Play' (states 1 and 2), you don't get the same problem. Even though the if statement means the exact same thing.
It's as though the code from the menu (state 0) is still being called, even though the state has been set to something else.
Can anyone help me with this? Thanks.
-
PFont scaryfont32, scaryfont40, scaryfont48;
-
int state = 0;
String highscorers[] = {"Dan", "Dan", "James", "Bill"};
int highscores[] = {100, 80, 55, 10}; -
//MenuMouseRolloverFunctions
boolean b1hover = false;
boolean b2hover = false;
boolean b3hover = false;
boolean b4hover = false; -
-
void setup() {
size(640, 480, P3D);
scaryfont32 = loadFont("OldEnglishTextMT-32.vlw");
scaryfont40 = loadFont("OldEnglishTextMT-40.vlw");
scaryfont48 = loadFont("OldEnglishTextMT-48.vlw"); -
}
-
void draw() {
/* -
MENU
-
*/
if (state == 0) {
background(15,30,50);
fill(255,255,255);
smooth();
ellipse(160,240,250,100);
ellipse(480,240,250,100);
ellipse(160,400,250,100);
ellipse(480,400,250,100);
textFont(scaryfont40);
text("Nightmare on Elms Street!", 100, 100);
textFont(scaryfont48);
//MenuMouseRolloverFunctions -
if (mouseX > 25 && mouseX < 300 && mouseY > 200 && mouseY < 280) {
b1hover = true; } else { b1hover = false;
}
if (mouseX > 350 && mouseX < 620 && mouseY > 200 && mouseY < 280) {
b2hover = true; } else { b2hover = false;
}
if (mouseX > 50 && mouseX < 250 && mouseY > 360 && mouseY < 435) {
b3hover = true; } else { b3hover = false;
}
if (mouseX > 350 && mouseX < 600 && mouseY > 360 && mouseY < 435) {
b4hover = true; } else { b4hover = false;
}
//MenuMouseClickFunctions
//Start Game
if (mousePressed && mouseX > 25 && mouseX < 300 && mouseY > 200 && mouseY < 280) {
state = 1;
}
if (b1hover == true) {
fill(255,0,0); } else { fill(150,0,0);
}
text("Start Game", 45, 255); -
//High Scores
if (mousePressed && mouseX > 350 && mouseX < 620 && mouseY > 200 && mouseY < 280) {
state = 2;
}
if (b2hover == true) {
fill(255,0,0); } else { fill(150,0,0);
}
text("High Scores", 355, 255);
//About
if (state == 0 && mousePressed && mouseX > 50 && mouseX < 250 && mouseY > 360 && mouseY < 435) {
state = 3;
}
if (b3hover == true) {
fill(255,0,0); } else { fill(150,0,0);
}
text("About", 100, 415);
//Exit
if (mousePressed && mouseX > 350 && mouseX < 600 && mouseY > 360 && mouseY < 435) {
exit();
}
if (b4hover == true) {
fill(255,0,0); } else { fill(150,0,0);
}
text("Exit", 430, 415);
} -
/* -
PLAY
-
*/
if (state == 1) {
background(0,0,15);
text("Play", 100, 100);
}
/* -
HIGH SCORES
-
*/
-
if (state == 2) {
background(0,0,15);
fill(255,255,255);
text("High Scores", 100, 100);
fill(150,0,0);
for (int i = 0; i < 4; i++) {
text(highscorers[i], 100, 150+(i*50));
}
}
/* -
ABOUT
-
*/
if (state == 3) {
background(0,0,0);
noSmooth();
translate(width / 2, height / 2);
// Orange point light on the right
pointLight(50, 200, 50, // Color
200, -150, 0); // Position -
// Blue directional light from the left
directionalLight(50, 50, 255, // Color
1, 0, 0); // The x-, y-, z-axis direction -
// Yellow spotlight from the front
spotLight(255, 50, 50, // Color
10000, 10000, 30000, // Position
0, -0.5, -0.5, // Direction
PI / 2, 2); // Angle, concentration
rotateY(map(mouseX, 375, width, 0, PI));
rotateX(map(mouseY, 375, height, 0, PI));
box(120);
textFont(scaryfont32);
fill(255,255,255);
//Daniel Ashley-Smith
text("Menu system and Intergration", -200, -100, 80);
text("Daniel Ashley-Smith", -150, -130, 90);
//William George
pushMatrix();
scale(1, -1);
text("In-game developer", -150, 100, -80);
text("William George", -100, 130, -90);
popMatrix();
//Thomas Riglar
pushMatrix();
translate(0, 75);
rotate(PI);
text("Sound Supervisor", -100, -50, 80);
text("Thomas Riglar", -150, -10, 90);
popMatrix();
//James Uren
pushMatrix();
scale(1, -1);
translate(0, 75);
rotate(PI);
text("Project Supervisor", -50, 190, -80);
text("James Uren", -50, 220, -90);
popMatrix(); -
}
} -
void keyPressed() {
if (key == '0') {
state = 0;
}
if (key == '1') {
state = 1;
}
if (key == '2') {
state = 2;
}
if (key == '3') {
state = 3;
}
if (state == 3 && key == 'q' || key == 'w' || key == 'e' || key == 'r' || key == 't' || key == 'y' || key == 'u' || key == 'i' || key == 'o' || key == 'p' ||
key == 'a' || key == 's' || key == 'd' || key == 'f' || key == 'g' || key == 'h' || key == 'j' || key == 'k' || key == 'l' ||
key == 'z' || key == 'x' || key == 'c' || key == 'v' || key == 'b' || key == 'n' || key == 'm' ||
key == '0' || key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9')
{ state = 0;
}
} -
void mousePressed()
{
if (state == 3 && mousePressed) {
state = 0;
}
}
1
