Hello @Chrisir
Much to my chagrin, I thought this was going to be a lot easier.
My thinking is to create a function in the CLASS and name it: move().
Then use the dot syntax in draw() as I did with mouseOver().
I’ve tried rewriting the if (key == CODED) section several different ways. The last 2 (failed) iterations remains in the CLASS for viewers’ reference.
At the very least, is this direction correct?
Comments most welcome!!
//////////////////////////////
// 2D Grid Array of Button objects
Button [][] myButtons;
// Number of columns and rows in the grid
int num = 10;
int cols = num, rows = cols;
color background = color (random(255), random(255), random(255));
void setup() {
size(500, 500);
int x = (width/num)/2, y = x;
myButtons = new Button[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Initialize each object
myButtons[i][j] = new Button (x+ i*(width/cols), y+ j*(height/rows),
color (random(255), random(255), random(255)), // random colors
color (random(255), random(255), random(255)), // new random colors when mouseOver
(width/num));
}
}
}
void draw() {
background(background);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
myButtons[i][j].mouseOver();
myButtons[i][j].display();
myButtons[i][j].move(); //move() commented out in class
}
}
}
///////////////////////////////////
class Button {
float x, y;
color colRect;
color colRect2;
float sz;
boolean off = true; // button starts in OFF position
Button (
float tempX, float tempY,
color tempColor, color tempColor2, float tempSz) {
x = tempX;
y = tempY;
colRect = tempColor;
colRect2 = tempColor2;
sz = tempSz;
}
void display() {
if (off) {
fill (colRect);
noStroke();
} else {
fill (colRect2);
noStroke();
}
rectMode(CENTER);
rect(x, y, sz, sz);
}
void mouseOver() {
if (mouseX > x - sz/2 &&
mouseX < x + sz/2 &&
mouseY > y - sz/2 &&
mouseY < y + sz/2) {
off = false;
} else {
off = true;
}
}
}
/*void move() { //trying to create a move function using cursor keys and failing
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
y = y+1;
} else if (keyCode == DOWN) {
y = y-1;
} else if (keyCode == LEFT) {
x = x-1;
} else (keyCode == RIGHT) {
x = x+1;
}
}
}
}
}
void keyPressed() {
if (keyCode== RIGHT)
x++;
if (keyCode== LEFT)
x--;
if (keyCode== UP)
y++;
if (keyCode== DOWN)
y--;
if (x < 0) x = 0;
if (x > width) x = width;
if (y < 0) y = 0;
if (y > height) y = height;
}*/