I'm new to coding and currently I'm in a class that has us use Processing 3 with java. I'm working on a project trying to set up a mousePressed() action, so that 3 static images appear but it's not showing up. (sorry if this is a stupid question).
Here's the code
PImage [] pics = new PImage [11];
int base=0;
int top=10;
int dollar=9;
boolean notPressed = true;
void setup() {
size(1200, 750);
background(255);
imageMode(CENTER);
for (int i=0; i<11; i++) {
pics[i] = loadImage("pic"+i+".png");
}
}
void draw() {
translate(500, 275);
if (notPressed) {
image(pics[int(random(1, 8))], 100, 100);
} else {
image(pics[base], 100, 100);
}
image(pics[top], 100, 100);
image(pics[dollar], 100, mouseY);
}
pushMatrix();
translate(500, 275);
image(pics[int(random(pics.length))], 100, 100);
popMatrix();
pushMatrix(); //moves dollar up and down
translate(500, 275);
image(pics[0], 100, 100);//base
image(pics[9], 100, mouseY);//dollar
popMatrix();
}
void mousePressed() {
notPressed=false;
}
void keyPressed() {
}
I suggest using a condition which changes when the mouse is pressed. You can do an
if
statement, slightly differently from what @YOUSFI Mohamet Walid suggested:In this version, there are basically three layers:
pics[0]
.pics[9]
) at the mouse level.After the mouse is clicked, the bottom layer no longer picks a random picture to show. Instead, it shows
pics[10]
every single frame.