We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello.
Is there a way to scan for multiple keys being pressed at the same time? I would like to be able to move UP + RIGHT, not only in the last direction pressed. Also, it is evident there is a small "gap" after the initial press of the key and the continuation, as there is when we are typing.
thank you all.
float x=width/2;
float y=height/2;
float xvel=0;
float yvel=0;
float frict = 0.97;
float vel = 0.6;
void setup() {
size(800, 800);
//fullScreen();
background(0);
frameRate(60);
x=width/2;
y=height/2;
}
void draw() {
background(0);//cls
x=x+xvel;
y=y+yvel;
yvel=yvel*frict;
xvel=xvel*frict;
rect(x, y, 50, 50);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
yvel=yvel-vel;
} else if (keyCode == DOWN) {
yvel=yvel+vel;
}
if (keyCode == LEFT) {
xvel=xvel-vel;
} else if (keyCode == RIGHT) {
xvel=xvel+vel;
}
}
}
Answers
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
http://studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
https://forum.Processing.org/two/discussions/tagged?Tag=keyreleased()
thank you.
Here is the above code to illustrate the changes.
float x=width/2; float y=height/2; float xvel=0; float yvel=0; float frict = 0.97; float vel = 0.6; boolean isLeft, isRight, isUp, isDown; void setup() { size(800, 800); //fullScreen(); background(0); frameRate(30); x=width/2; y=height/2; } void draw() { background(0);//cls x=x+xvel; y=y+yvel; if (isLeft)xvel=xvel-frict; if (isRight)xvel=xvel+frict; if (isDown)yvel=yvel+frict; if (isUp)yvel=yvel-frict; yvel=yvel*frict; xvel=xvel*frict; rect(x, y, 50, 50); } void keyPressed() { setMove(keyCode, true); } void keyReleased() { setMove(keyCode, false); } boolean setMove(int k, boolean b) { switch (k) { case UP: return isUp = b; case DOWN: return isDown = b; case LEFT: return isLeft = b; case RIGHT: return isRight = b; default: return b; } }