I’m currently trying to make a program that shows a grid of tiles that change color when clicked. I’ve made it so that the tiles are evenly distributed with an edge or gutter around the sides no matter the number of tiles or size of the window. It looks like the image attached.
The problem arises when I try to perform the clicking action on the tiles. I have mouseClicked() call a function inside every tile, passing the mouseX and mouseY position (I have no issues with the y position) in to determine if that tiles column has been clicked, turning those tiles green if it has been clicked. The issue is that when clicking, the column exactly to the RIGHT of the one clicked is the one that turns green. Here’s what it looks like if I click the second left most column:
Another strange but I’m assuming telling part of the issue i’m having is that if I click the column on the farthest right side, the tiles on the farthest left side turn green, almost as if my click warped through the wall and back to the other side like in pacman.
When having the program print out my mouseX position and the clicked columns boundaries, it tells me that the mouseX coordinate was indeed where it should of been, yet the column to the right of that one is the one that turns green. This is the code in my Tile class that checks if the mouse was in that column when it is called:
public void flipped(float clickX, float clickY){
if(clickX > x && clickX < x+width){// && clickY > y && clickY < y + height){
flipped = !flipped;
//System.out.println("Click X: " + clickX + " Left edge: " + x + " Right edge: " + (int)(width+x));
}
}
Here’s the code inside the Tiles class (the class that creates an object to hold all the tiles and instantiate them) that fills the array with tiles in the correct position:
public void initTiles(){
float x;
float y;
//For every column,...
for(int c = 0; c < tilePerCol; ++c) {
//y is one gutter height apart from the last tile
y = c * (tileHeight+gutterHeight) + gutterHeight;
//for every row...
for(int r = 0; r < tilePerRow; ++r){
//x is one gutter width from the last tile, starting at 0 for each row in a column
x = r*(gutterWidth+tileWidth) + gutterWidth;
tiles.add(new Tile(p, tileWidth, tileHeight, x, y));
}
}
// System.out.println(tiles.size());
}
I have tried subtracting the width from where I check the tile click detection, which works for everything except the farthest left column, which cannot be interacted with using that fix.
I have also tried messing with collision boundaries which showed itself to not be the problem.
I’m not sure how this is happening, because the click detection is handled by each tile individually, so the issue isn’t as simple as that it’s starting at the wrong index.
I would appreciate any advice, i’m spent on trying to figure out what is going on here, as I’m sure it’s glaringly obvious.
Thanks,