change colour on mouse press

edited March 2018 in Questions about Code

I have got to this point, using help from this post

and have changed it a bit to get the following:

color[] colours = new color[3]; int c = 0;

void setup() { size(500, 500); colours[0] = color(255, 0, 0); colours[1] = color(0, 255, 0); colours[2] = color(0, 0, 255); }

void draw() { if (mousePressed == true && (mouseButton == LEFT)) {


ellipse(mouseX, mouseY, 50, 50);

c = (c + 1) % 3;

} }

However, when I click the left mouse button, it continuously displays circles until the mouse click is released. Instead I wish it to display one circle of one colour per click. I.E. 1st click = red circle at mouse coordinates, release mouse click 2nd click = green circle at mouse coordinates, release mouse click 3rd click = blue circle at mouse coordinates, release mouse click 4th click = returns to red circle, release mouse click And so on...

How would I go about doing this?

Thanks for any help.



  • edited March 2018 Answer ✓

    Instead of using mousePressed as a variable use mousePressed () as a function

    See reference. The function is indicated by the brackets ()

    The function registers mouse clicks only once but the variable registers them throughout, often

  • edited March 2018

    I just tried to do that but I made a mess of it. Sorry, I am relatively new. How would I do that? Is it changing it back to how the post I linked had it?


  • edited March 2018

    A function is a section from { to }.

    You have functions setup() and draw() now.

    As shown in the reference make a new function mousePressed() after draw() {.....} :

    void mousePressed () {
          // move the entire if section from draw here in this spot
  • edited March 2018

    Then check your brackets { }

    You can save with a new name then you have two distinct versions of your sketch

  • That worked. Thank you very much!

Sign In or Register to comment.