key event troubles
in
Programming Questions
•
2 years ago
I'm trying to create a simple application where a rect is drawn on the screen and moved by keys. 'a' is supposed to move the rect down while 's' is supposed to move it upwards. For some reason after I press 'a' and the rect stops at the bottom, 's' takes over and key 'a' becomes unresponsive... Any ideas how the sketch could be improved?
PVector loc;
PVector acc;
PVector acc2;
boolean stop, a, s;
void setup() {
size(400, 400);
loc = new PVector(200, 200);
acc = new PVector(0, 3);
//acc2 = new PVector(0, -1);
}
void draw() {
background(255);
rect(loc.x, loc.y, 30, 30);
if (a) {
loc.add(acc);
}
if (s) {
loc.sub(acc);
}
if (loc.y>height-30) {
loc.y = height-30;
}
else if (loc.y<=0) {
loc.y=0;
}
if (s) {
loc.sub(acc);
}
println(loc.y);
}
void keyPressed() {
if (key == 'a') {
a=!a;
}
if (key == 's') {
s=!s;
}
}
1