simple mouse click problem
in
Programming Questions
•
3 years ago
What I currently have is a square that is following a "terrain". When the mouse is clicked anywhere on the screen, the square is grabbed. When the mouse is clicked again, it begins to walk everywhere on the screen.
What I would want to happen is that after the mouse is clicked and the square is grabbed that when the mouse is clicked again, the square starts at the left hand side of the screen and follows the terrain once again.
I tried several things to no avail.
I was thinking that the problem was in the 'if' statement associated with the boolean mouseClick. But I'm not too sure.
I'm just trying to learn processing and I appreciate any help! :)
code is posted below:
boolean mouseClick = false;
float x = 0;
float y = 40;
float rectSize = 10;
float speed = 0.5;
int counter = 0;
void setup() {
size(200,200);
}
void draw() {
background(255);
//terrain
noFill();
stroke(150);
beginShape();
vertex(0,50);
vertex(30,50);
vertex(30,130);
vertex(80,130);
vertex(80,170);
vertex(width,170);
endShape();
//rectangle
noStroke();
fill(0);
rect(x,y,rectSize,rectSize);
if (counter == 0) {
x += speed;
if (x == 30) {
counter = 1;
}
}
else if (counter == 1) {
y += speed;
if (y == 120) {
counter = 2;
}
}
else if (counter == 2) {
x += speed;
if (x == 80) {
counter = 3;
}
}
else if (counter == 3) {
y += speed;
if (y ==160) {
counter = 4;
}
}
else if (counter == 4) {
x += speed;
if (x == width) {
counter = 0;
x = 0;
y = 40;
}
}
if (mouseClick) {
x = mouseX;
y = mouseY;
}
}
void mousePressed () {
mouseClick = !mouseClick;
}
What I would want to happen is that after the mouse is clicked and the square is grabbed that when the mouse is clicked again, the square starts at the left hand side of the screen and follows the terrain once again.
I tried several things to no avail.
I was thinking that the problem was in the 'if' statement associated with the boolean mouseClick. But I'm not too sure.
I'm just trying to learn processing and I appreciate any help! :)
code is posted below:
boolean mouseClick = false;
float x = 0;
float y = 40;
float rectSize = 10;
float speed = 0.5;
int counter = 0;
void setup() {
size(200,200);
}
void draw() {
background(255);
//terrain
noFill();
stroke(150);
beginShape();
vertex(0,50);
vertex(30,50);
vertex(30,130);
vertex(80,130);
vertex(80,170);
vertex(width,170);
endShape();
//rectangle
noStroke();
fill(0);
rect(x,y,rectSize,rectSize);
if (counter == 0) {
x += speed;
if (x == 30) {
counter = 1;
}
}
else if (counter == 1) {
y += speed;
if (y == 120) {
counter = 2;
}
}
else if (counter == 2) {
x += speed;
if (x == 80) {
counter = 3;
}
}
else if (counter == 3) {
y += speed;
if (y ==160) {
counter = 4;
}
}
else if (counter == 4) {
x += speed;
if (x == width) {
counter = 0;
x = 0;
y = 40;
}
}
if (mouseClick) {
x = mouseX;
y = mouseY;
}
}
void mousePressed () {
mouseClick = !mouseClick;
}
1