How to make an object change color when the mouse is inside?

alxalx
edited December 2014 in How To...

Hi, could you help me, I´m beginner on this and I would like how to make an object change color when the mouse is inside and how apply this to several objects in the same program, I got this:

void setup() { size(500,400); }

void draw() { background(0);

stroke(1); fill(170); rect(200, 150, 100, 100); }

void mouseMoved (){

stroke(1); fill (232, 287, 146); rect(200, 150, 100, 100);

}

Thanks!!!

Tagged:

Answers

  • edited December 2014

    in draw() use if to check if mouse is inside rect. if so, set is to one color, if not to another.

    if (mouseX>200 && mouseX < 200+100 && 
    mouseY>150 && mouseY < 150+100) 
        fill(170); 
    else
        fill(232,287,146);
    

    rect.....

    to do this with multiple rects just put them one after another

    or: you store the rects in an array and use for-loop to go over them

    later use oop - see tutorial.

    ;-)

Sign In or Register to comment.