Stop movement
in
Programming Questions
•
1 year ago
Hi,
The lines below are from a sketch with an object that moves to the left and right of the screen and accelerates when the mousepointer is further to the right. It is supposed to stop moving when I press a key but I'm not sure what I should type at keyPressed, I'm supposed to use the boolean canMove. I've tried numerous things but I can't figure it out. Can anyone help me out?
int x = 100;
boolean horizontal = false;
boolean canMove= false;
int speed = 1;
void setup() {
size(200, 200);
}
void draw() {
background(237, 2, 2);
drawMovement();
}
void keyPressed() {
}
}
void drawMovement() {
speed= mouseX/10;
background(237, 2, 2);
if (x > width) {
horizontal = !horizontal;
}
if (x < 0) {
horizontal = !horizontal;
}
if (horizontal == true) {
x = x + speed;
}
else {
x = x-speed;
}
speed = mouseX/10;
}
The lines below are from a sketch with an object that moves to the left and right of the screen and accelerates when the mousepointer is further to the right. It is supposed to stop moving when I press a key but I'm not sure what I should type at keyPressed, I'm supposed to use the boolean canMove. I've tried numerous things but I can't figure it out. Can anyone help me out?
int x = 100;
boolean horizontal = false;
boolean canMove= false;
int speed = 1;
void setup() {
size(200, 200);
}
void draw() {
background(237, 2, 2);
drawMovement();
}
void keyPressed() {
}
}
void drawMovement() {
speed= mouseX/10;
background(237, 2, 2);
if (x > width) {
horizontal = !horizontal;
}
if (x < 0) {
horizontal = !horizontal;
}
if (horizontal == true) {
x = x + speed;
}
else {
x = x-speed;
}
speed = mouseX/10;
}
1