keyPressed and multiple instances
in
Programming Questions
•
1 year ago
What I basically want is to have a few different states controlled by three keys m, k, and q.
So if 'm' is pressed it would bring the program into 1st state. If 'k' is pressed 2nd state. if 'q' pressed 3rd state.
Once one of the above states is chosen (let's say 1st state) the user would have another opportunity to choose m, k, or q within that state. So 1st state + m = do this, 1st state + k = do something else, and 1st state + q = do another thing.
The issue I'm running into is if m is pressed at all the program doesn't remember if it's already in the 1st state so it will just start over and go back into 1st state instead of doing the second request. Here is what my code looks like but 1st, 2nd and 3rd states are defined as Forward, Left and Right.
int gameState;
int FORWARD = 0;
//int 1left = 1;
//int 1right = 2;
void setup() {
size(600, 600);
background(0);
}
void draw() {
smooth();
if(keyPressed) {
if (key == 'm') {
background(255);
gameState = FORWARD;
} else if (key == 'k') {
background(150);
//gameState = 1left;
} else if (key == 'q') {
background(255, 0, 0);
//gameState = 1right;
}
}
if (gameState == FORWARD) {
forward();
}
}
void forward() {
if (key == 'm') {
// forward choice
ellipse(300, 300, 50, 50);
} else if (key == 'k') {
//left choice
rect(200, 200, 10, 10);
} else if (key == 'q') {
// right choice
ellipse(10, 10, 30, 30);
}
}
I'm wondering is there a way for the program to remember it's already in a state or possibly counting the keyPresses?? So if m is pressed once do this if m is pressed twice do this?
Hope I made sense.
So if 'm' is pressed it would bring the program into 1st state. If 'k' is pressed 2nd state. if 'q' pressed 3rd state.
Once one of the above states is chosen (let's say 1st state) the user would have another opportunity to choose m, k, or q within that state. So 1st state + m = do this, 1st state + k = do something else, and 1st state + q = do another thing.
The issue I'm running into is if m is pressed at all the program doesn't remember if it's already in the 1st state so it will just start over and go back into 1st state instead of doing the second request. Here is what my code looks like but 1st, 2nd and 3rd states are defined as Forward, Left and Right.
int gameState;
int FORWARD = 0;
//int 1left = 1;
//int 1right = 2;
void setup() {
size(600, 600);
background(0);
}
void draw() {
smooth();
if(keyPressed) {
if (key == 'm') {
background(255);
gameState = FORWARD;
} else if (key == 'k') {
background(150);
//gameState = 1left;
} else if (key == 'q') {
background(255, 0, 0);
//gameState = 1right;
}
}
if (gameState == FORWARD) {
forward();
}
}
void forward() {
if (key == 'm') {
// forward choice
ellipse(300, 300, 50, 50);
} else if (key == 'k') {
//left choice
rect(200, 200, 10, 10);
} else if (key == 'q') {
// right choice
ellipse(10, 10, 30, 30);
}
}
I'm wondering is there a way for the program to remember it's already in a state or possibly counting the keyPresses?? So if m is pressed once do this if m is pressed twice do this?
Hope I made sense.
1