Having problems with states in my code
in
Core Library Questions
•
2 months ago
Hi, im designing a game at the moment and i have created several different states for differing pages in the game. As outlined at the bottom. The problem I'm having at the moment is when i click on a button in say state 1 (the menu), it also clicks the button on the next page and jumps a state.
How do i stop this from happening because its bugging me a lot.
As you can see ive copy and pasted the code sorry if its rather long winded and untidy, have only been programming a year.
thanks
int y;
int state = 0;
import ddf.minim.*;
AudioPlayer player1;
AudioPlayer player2;
Minim minim;
PFont candarabold;
PImage bg;
PImage bg2;
PImage bg3;
PImage bg4;
PImage gameplaybackground;
PImage manutdlogo;
PImage arsenallogo;
float oneplayerPosX;
float oneplayerPosY;
float twoplayerPosX;
float twoplayerPosY;
float rulesPosX;
float rulesPosY;
float exitPosX;
float exitPosY;
float boxWidth;
float boxHeight;
float boxWidth2;
float boxHeight2;
float manutdPosX;
float manutdPosY;
float arsenalPosX;
float arsenalPosY;
void setup() {
size(1000,600);
smooth();
minim = new Minim(this);
player1 = minim.loadFile("lovin each day.mp3");
player1.play();
bg = loadImage("gamebackground.jpg");
bg2 = loadImage("gamebackground2.jpg");
bg3 = loadImage("gamebackground3.jpg");
bg4 = loadImage("gamebackground4.jpg");
gameplaybackground = loadImage("gameplaybackground.jpg");
manutdlogo = loadImage("manutdlogo.png");
arsenallogo = loadImage("arsenallogo.png");
oneplayerPosX = 580;
oneplayerPosY = 110;
twoplayerPosX = 580;
twoplayerPosY = 230;
rulesPosX = 580;
rulesPosY = 350;
exitPosX = 580;
exitPosY = 470;
boxWidth = 270;
boxHeight = 80;
boxWidth2 = 350;
boxHeight2 = 70;
manutdPosX = 610;
manutdPosY = 80;
arsenalPosX = 610;
arsenalPosY = 180;
}
void home() {
candarabold = loadFont("Candara-Bold-48.vlw");
background(bg);
textFont(candarabold);
fill(0);
textSize(45);
text("Click to Start", 560, 360);
if(mousePressed){
state = 1;
}
}
void menu() {
background(bg3);
fill(255);
rect(oneplayerPosX, oneplayerPosY, boxWidth, boxHeight);
rect(twoplayerPosX, twoplayerPosY, boxWidth, boxHeight);
rect(rulesPosX, rulesPosY, boxWidth, boxHeight);
rect(exitPosX, exitPosY, boxWidth, boxHeight);
fill(0);
textSize(40);
text("One Player", 622, 164);
text("Two Player", 622, 285);
text("Game Rules", 615, 405);
text("Exit", 680, 525);
if(mousePressed){
if(mouseX > oneplayerPosX && mouseX < oneplayerPosX + boxWidth && mouseY >
oneplayerPosY && mouseY < oneplayerPosY + boxHeight)
{
state = 2;
}
}
if(mouseX > oneplayerPosX && mouseX < oneplayerPosX + boxWidth && mouseY >
oneplayerPosY && mouseY < oneplayerPosY + boxHeight)
{
fill(200,0,0);
rect(oneplayerPosX, oneplayerPosY, boxWidth, boxHeight);
fill(255);
text("One Player", 622, 164);
}
else
{
fill(255);
rect(oneplayerPosX, oneplayerPosY, boxWidth, boxHeight);
fill(0);
text("One Player", 622, 164);
}
if(mousePressed){
if(mouseX > twoplayerPosX && mouseX < twoplayerPosX + boxWidth && mouseY >
twoplayerPosY && mouseY < twoplayerPosY + boxHeight)
{
state = 0;
}
}
if(mouseX > twoplayerPosX && mouseX < twoplayerPosX + boxWidth && mouseY >
twoplayerPosY && mouseY < twoplayerPosY + boxHeight)
{
fill(200,0,0);
rect(twoplayerPosX, twoplayerPosY, boxWidth, boxHeight);
fill(255);
text("Two Player", 622, 285);
}
else
{
fill(255);
rect(twoplayerPosX, twoplayerPosY, boxWidth, boxHeight);
fill(0);
text("Two Player", 622, 285);
}
if(mousePressed){
if(mouseX > rulesPosX && mouseX < rulesPosX + boxWidth && mouseY >
rulesPosY && mouseY < rulesPosY + boxHeight)
{
state = 0;
}
}
if(mouseX > rulesPosX && mouseX < rulesPosX + boxWidth && mouseY >
rulesPosY && mouseY < rulesPosY + boxHeight)
{
fill(200,0,0);
rect(rulesPosX, rulesPosY, boxWidth, boxHeight);
fill(255);
text("Game Rules", 615, 405);
}
else
{
fill(255);
rect(rulesPosX, rulesPosY, boxWidth, boxHeight);
fill(0);
text("Game Rules", 615, 405);
}
if(mousePressed){
if(mouseX > exitPosX && mouseX < exitPosX + boxWidth && mouseY >
exitPosY && mouseY < exitPosY + boxHeight)
{
exit();
}
}
if(mouseX > exitPosX && mouseX < exitPosX + boxWidth && mouseY >
exitPosY && mouseY < exitPosY + boxHeight)
{
fill(200,0,0);
rect(exitPosX, exitPosY, boxWidth, boxHeight);
fill(255);
text("Exit", 680, 525);
}
else
{
fill(255);
rect(exitPosX, exitPosY, boxWidth, boxHeight);
fill(0);
text("Exit", 680, 525);
}
}
void oneplayer() {
background(bg4);
fill(235,0,0);
rect(manutdPosX, manutdPosY, boxWidth2, boxHeight2);
if(mousePressed){
if(mouseX > manutdPosX && mouseX < manutdPosX + boxWidth2 && mouseY >
manutdPosY && mouseY < manutdPosY + boxHeight2)
{
state = 3;
}
}
if(mouseX > manutdPosX && mouseX < manutdPosX + boxWidth2 && mouseY >
manutdPosY && mouseY < manutdPosY + boxHeight2)
{
fill(255);
rect(manutdPosX, manutdPosY, boxWidth2, boxHeight2);
fill(235,0,0);
text("Man United", 730, 130);
}
else
{
fill(235,0,0);
rect(manutdPosX, manutdPosY, boxWidth2, boxHeight2);
fill(255);
text("Man United", 730, 130);
}
image(manutdlogo,610,85);
fill(205);
rect(arsenalPosX, arsenalPosY, boxWidth2, boxHeight2);
if(mousePressed){
if(mouseX > arsenalPosX && mouseX < arsenalPosX + boxWidth2 && mouseY >
arsenalPosY && mouseY < arsenalPosY + boxHeight2)
{
state = 3;
}
}
if(mouseX > arsenalPosX && mouseX < arsenalPosX + boxWidth2 && mouseY >
arsenalPosY && mouseY < arsenalPosY + boxHeight2)
{
fill(#FFDB58);
rect(arsenalPosX, arsenalPosY, boxWidth2, boxHeight2);
fill(185,0,0);
text("Arsenal", 730, 230);
}
else
{
fill(205,0,0);
rect(arsenalPosX, arsenalPosY, boxWidth2, boxHeight2);
fill(255);
text("Arsenal", 730, 230);
}
image(arsenallogo,623,187);
}
void oneplayerstart() {
background(gameplaybackground);
player2 = minim.loadFile("crowd.mp3");
player1.close();
player2.play();
}
void draw() {
if (state==0)
{
home();
}
if (state==1)
{
menu();
}
if (state==2)
{
oneplayer();
}
if(state==3)
{
oneplayerstart();
}
}
1