Hi everyone!
I created a grid of rects using a 2d array. I now want to be able to click on a rect and have it change its color. With the code I have now, all rects change color when clicked. What am I doing wrong?
int no = 16;
int rectW;
int rectH;
int cols = no / 2;
int rows = no / 2;
int[][] rects;
color p = 255;
void setup() {
size(600,600);
background(150);
rectW = width / rows;
rectH = height / cols;
rects = new int[cols][rows];
}
void draw() {
background(150);
drawGrid();
}
void drawGrid() {
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
fill(p);
rect(i*rectW,j*rectH, rectW, rectH, 5);
}
}
}
void mouseClicked() {
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
int x = i*rectW;
int y = j*rectH;
if(mouseX >= x && mouseX <= (x+rectW) && mouseY >= y && mouseY <= (y+rectH)) {
p = 0; }
}
}
}