Question about collision
in
Programming Questions
•
6 months ago
Hello, I'm currently working on a project that requires a grid to be laid out across the sketch, the user click on different tiles of the grid, and then a line moving across the screen be able to interact with only the clicked objects. I was wondering what the best way to go about this would be? Here's the code I have so far, it's just a column of the tiles instead of the full grid:
Tile [] tiles;
int spacer = 100;
color c = color(170, 170, 170);
void setup() {
size(400, 400);
tiles = new Tile[4];
for (int i=0; i < 4; i++) {
tiles[i] = new Tile(0, spacer*i, spacer);
}
}
void draw() {
background(255);
for (int i = 0; i<tiles.length; i++) {
tiles[i].display();
}
}
void mouseReleased() {
for (int i = 0; i<tiles.length; i++) {
Tile tile = tiles[i];
if (mouseX > tile.x &&
mouseX < tile.x + tile.w &&
mouseY > tile.y &&
mouseY < tile.y + tile.h) {
tile.pressed = !tile.pressed;
}
}
}
/////////////////Tile Class
class Tile {
int x, y, spacer;
boolean pressed;
int w, h;
color c = color(170, 170, 170);
Tile(int tempX, int tempY, int tempSpace) {
w = tempSpace;
h = tempSpace;
y = tempY;
x = tempX;
}
void display() {
fill(c);
rect(x, y, w, h);
if (pressed) {
c = color(170, 170, 0);
}
else {
c = color(170, 170, 170);
}
}
}
1