selecting one object from an array
in
Programming Questions
•
10 months ago
Hi,
So I have an array in which I create 'ants'. What I want to do is to be able to select one ant on the screen with a mouseclick and have a small rectangle around it so that you know you've selected it. This is the code I thought would do the job:
void collision() {
for(int i=0;i<AntGroup.size();i++) {
Ant eachAnt = (Ant)AntGroup.get(i);
if (((dist(mouseX, mouseY, eachAnt.antX, eachAnt.antY) < 20.0) && mouseButton == LEFT) == true) {
stroke(204, 102, 0);
noFill();
rect(eachAnt.antX, eachAnt.antY, 15, 15);
clicked = true;
} if (clicked == true) {
stroke(204, 102, 0);
noFill();
rect(eachAnt.antX, eachAnt.antY, 15, 15);
} if ((dist(mouseX, mouseY, eachAnt.antX, eachAnt.antY) < 20.0) && mouseButton == RIGHT) {
clicked = false;
}
}
}
The problem is that now all the ants get selected instead of one. Does anybody know how to solve this. I'm kind of running against a deadline... aahh!
Samuel
So I have an array in which I create 'ants'. What I want to do is to be able to select one ant on the screen with a mouseclick and have a small rectangle around it so that you know you've selected it. This is the code I thought would do the job:
void collision() {
for(int i=0;i<AntGroup.size();i++) {
Ant eachAnt = (Ant)AntGroup.get(i);
if (((dist(mouseX, mouseY, eachAnt.antX, eachAnt.antY) < 20.0) && mouseButton == LEFT) == true) {
stroke(204, 102, 0);
noFill();
rect(eachAnt.antX, eachAnt.antY, 15, 15);
clicked = true;
} if (clicked == true) {
stroke(204, 102, 0);
noFill();
rect(eachAnt.antX, eachAnt.antY, 15, 15);
} if ((dist(mouseX, mouseY, eachAnt.antX, eachAnt.antY) < 20.0) && mouseButton == RIGHT) {
clicked = false;
}
}
}
The problem is that now all the ants get selected instead of one. Does anybody know how to solve this. I'm kind of running against a deadline... aahh!
Samuel
1