Some remarks:
- You should put the loadImage calls in setup, in an array of PImage for example. loadImage is a slow operation (reading a file on disk) that shouldn't be done repetitively.
- draw is called several times per second. So it will probably execute the code in mousePressed condition (loading a file) several times on each click.
- You didn't made the modes exclusive. So after setting mode == 1, you will enter the second condition, without having the time to see the second image.
A possible rewrite would look like:
Code:PImage[] images = new PImage[3];
PImage img;
int mode = 0;
void setup() {
size (200,200);
images[0] = loadImage("1.jpg");
images[1] = loadImage("2.jpg");
images[2] = loadImage("3.jpg");
img = images[0];
}
void draw() {
background(img);
if (mousePressed) {
if (mode == 0) {
img = image[1];
mode = 1;
} else if (mode == 1) {
img = image[2];
mode = 2;
} else if (mode == 3) {
img = image[0];
mode = 0;
}
}
}
I wanted to illustrate my point by keeping close of your code, but a simpler version would be:
Code:void draw() {
background(image[mode]);
if (mousePressed) {
mode = (mode + 1) % images.length;
}
}
But mode can map differently to the array of images.
Actually, above code is probably (I don't test!) still not working as you want, as mode keeps changing as long as you keep the mouse pressed.
So you should move the mode changing logic to mousePressed() event routine:
Code:void draw() {
background(image[mode]);
}
void mousePressed() {
mode = (mode + 1) % images.length;
}