We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I have a switch/case that is cycling through 3 images within the same coordinates when the mouse is pressed. My issue is that as soon as the condition in case 1 is met (mouse tapped), the switch/case cycles through all the cases until the last one. Here is my code below:
int counter_channels =1;
switch(counter_channels) {
case 1:
channelOne.display(0, 0);
if (mouseX > displayWidth/1.2-channelOne.getWidth() && mouseX < displayWidth/1.2+channelOne.getWidth() &&
mouseY > displayHeight/1.55-channelOne.getHeight() && mouseY < displayHeight/1.55+channelOne.getHeight()) {
counter_channels++;
}
break;
case 2:
channelTwo.display(0, 0);
if (mouseX > displayWidth/1.2-channelTwo.getWidth() && mouseX < displayWidth/1.2+channelTwo.getWidth() &&
mouseY > displayHeight/1.55-channelTwo.getHeight() && mouseY < displayHeight/1.55+channelTwo.getHeight() ) {
counter_channels++;
}
break;
case 3:
int xposTV= 171;
int yposTV = 150;
fill(127);
rect(0, 0, xposTV, yposTV);
}
Answers
Let me guess: you call this code from draw(), not from mousePressed(), right?
eek. I didn't realize that this needed to be nested in the function mousePressed. Unless my response is premature...
If you check this in draw(), this is checked 60 times per second (the default framerate), and a click is usually longer, so it spawns over several frames, hence the cycling you see.
mousePressed() is called only once per click.
Lot of people make this mistake, hence my guess...
And since you are not the first one to make this mistake, it inspired me to a new Technical FAQ entry: http://wiki.processing.org/w/Why_is_my_mouse_pressed_code_executed_several_times?
Much appreciated!