Hi,
so I’m trying to make a small game in which you control a litlle space ship and dodge some stuff. Something like that. The player should be able to move in 8 directions, including the 4 diagonal ones. I guess you can imagine what I mean by that. There’s only one problem. Processing only seems to register one input at a time the way I wrote my code. For example, it’s possible to move up or left by pressing the arrwow keys, but not both at the same time, How can I do that?
I don’t know if you just want a hint at to how to do it yourself, or just a working code, so i’ll blur the code below the Spoiler sign and just give you some hints first
Processing takes input only with keyCode as far as i know, so theres not much you can do if you have multiple inputs at once, other than getting them one after the other. And thats impossible if you don’t have superhuman reflexes, so you have to save the input somehow, at least for the duration of that frame. Example, pressing left and right should not give you any result, but it will, because it will tell processing keyCode = left and then keyCode = right. Even if you are still holding left, once you press right keyCode is right. So you could navigate right by pressing left. What would be best in you case would be to save the input you can get. I.e: Up, Down, Left, Right. and then, every move just execute the ones that you got and that didn’t get turned down. Best would be to use Booleans. This way you can also move diagonally. Btw, theres also a keyReleased option, which you need to do this. Now to the Code:
Spoiler : (just click on it to show it)
float[] plane = {350, 700, 50, 70}; //x,y,width,height
boolean up, down, left, right;
void setup() {
size(700, 800);
rectMode(CENTER);
}
void draw() {
background(0);
movePlane();
plane();
}
void plane() {
rect(plane[0], plane[1], plane[2], plane[3]);
}
void movePlane() {
if (left) {
plane[0] -= 5;
}
if (up) {
plane[1] -= 5;
}
if (right) {
plane[0] += 5;
}
if (down) {
plane[1] += 5;
}
}
void keyPressed() {
// this basically means if (keyCode == UP) { up = true; } else { up = up; }
//you can add the last part (up; down; and so) into false, then it will only get one input at once again.
up = keyCode == UP ? true : up;
down = keyCode == DOWN ? true : down;
left = keyCode == LEFT ? true : left;
right = keyCode == RIGHT ? true : right;
}
void keyReleased() {
up = keyCode == UP ? false : up;
down = keyCode == DOWN ? false : down;
left = keyCode == LEFT ? false : left;
right = keyCode == RIGHT ? false : right;
}
Create an array int[] keys = new int[1000]; Then in have
void keyPressed() {
keys[keyCode] = true; // If you press the key it will be true in it's index in keys
}
void keyReleased() {
keys[keyCode] = false; // When you let go it will return to false again
}
And if you want to know when keys are being pressed
if(keys[<Your keyCode>]) { // Check if the key is activated
... Code ...
}
This will also work with all the keys, as the array stores all of the keyCode's.
If i got what you mean, then i think that should be a boolean array, not int. But if you only have 4 Inputs it is kinda Overkill to have an Array with 1000 Elements in it.
Following up on @Sarah’s answer – I have shared a HashMap solution to this problem and ones like this a few times in the past. The HashMap approach is a bit nicer than int[1000] in that it can store any key you press, but ONLY stores the keys you press. Then instead of checking for keys, you do this: