mousePressed not working - simple code

edited November 2016 in Questions about Code

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.

  • edited November 2016

    Sorry!

    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);}}
    
    }
    
  • edited November 2016

    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!

  • edited November 2016 Answer ✓
    • Like I said before, you've mostly got 2 options: a standard 1 and an advanced 1. :(|)
    • Processing API offers a buncha callback functions, like setup(), draw(), keyPressed(), etc.
    • They're all invoked automatically by the sketch at their right triggered moment.
    • And they all belong to the API's main class PApplet.
    • However, your own mousePressed() method there belongs to class Tear instead. #-o
    • Thus the sketch can't find it unless it's registered by undocumented function registerMethod(). ~:>
    • The standard approach is to simply invoke your own mousePressed() method for each of its instances inside sketch's mousePressed() callback: *-:)

    https://Processing.org/reference/mousePressed_.html
    https://Processing.org/reference/for.html

    void mousePressed() {
      for (final Tear tear : drops)  tear.mousePressed();
    }
    
Sign In or Register to comment.