We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! I'm new to both coding and the boards so please bear with me. I'm trying to make a simple code where circles, which are supposed to represent tear drops, fall and the person controlling the mouse cursor, which takes on the image of a bucket, clicks on the tears to make them disappear, to make the illusion of tears falling into the bucket.
I'm having trouble with the mousePressed command. I cannot seem to be able to make the falling circles disappear when clicked on. Does anyone have any suggestions to help get me on the right track?
Here is what I have:
Tear t1;
PImage mouseCursor;
int numDrops = 80;
Tear[] drops = new Tear[numDrops]; // This creates the array of tears - how many will be able to fall.
void setup() {
size(1000,800);
background(0);
smooth();
noStroke(); // This loops the array of tears.
for (int i = 0; i < drops.length; i++) {
drops[i] = new Tear(); // This creates the tears as objects.
t1 = new Tear();
}
mouseCursor = loadImage("bucket.png"); // This loads the image of the bucket as the cursor.
}
void draw(){
fill(0,80);
rect(0,0,1000,800); // This loops the array of tears.
for (int i = 0; i < drops.length; i++) {
drops[i].fall();
}
if(mouseX < 1000) { // This command makes the cursor the bucket image at all times.
cursor(mouseCursor, 0, 0);
} else {
cursor(HAND);
}
}
class Tear { // This allows the tears to be able to fall randomly at random positions on the screen.
float t = random(1000);
float y = random(-height);
void fall() {
y = y + 10;
fill(0,139,211,255); // The tears themselves. :'(
ellipse(t, y, 10, 10);
if(y>height){
t = random(1000);
y = random(-200);
}
}
void mousePressed(){
;if (mouseX >= t && mouseX <= t + width &&
mouseY >= y && mouseY <= y+ height){
fill(0);
} else {
fill(0,139,211,255);}}
}
Thanks!
Answers
Please highlight the code properly.
Use ctrl-t in the processing editor to indent it. Then cut it, paste it into a message, highlight it and press ctrl-o
Without that we can't tell whether your mousePressed is within the Tear class or outside it. It should be outside.
Sorry!
Callback mousePressed() belongs to PApplet top sketch.
It doesn't work inside our own custom classes, unless we registerMethod() it. :-@
Does that mean I have to place the mousePressed command somewhere else? Or do I have to add extra code?
I am an inexperienced coder, so I do not know much. I'm very sorry!
https://Processing.org/reference/mousePressed_.html
https://Processing.org/reference/for.html