1
votes

I have a 2D button array (buttons) that will generate a 5 by 5 grid of buttons. I want to get the index of a single button (say, (2,2) in the center) when clicked, and find the index values of buttons surrounding the original button within a 3 x 3 radius by creating integer variables such as topcentre (which would be the index of buttons(x, y - 1) or 1,2, where x and y are the values of the clicked button.) I can then add text, etc. to those surrounding buttons.

Here's a visualization:

0,0|0,1|0,2|0,3|0,4

1,0|1,1|1,2|1,3|1,4

2,0|2,1|2,2|2,3|2,4

3,0|3,1|3,2|3,3|3,4

4,0|4,1|4,2|4,3|4,4

How can I do this?

1

1 Answers

3
votes

In the backend you could just use a single array.

0,0|0,1|0,2|0,3|0,4|1,0|1,1|1,2|1,3|1,4|2,0|2,1|2,2|2,3|2,4|3,0|3,1|3,2|3,3|3,4|4,0|4,1|4,2|4,3|4,4

Even if on the UI it's display as 2D.

2,2 would be index 12 ({x} * {width} + {y} = 2 * 5 + 2 = 12). To get the other index, you can substract or add.

Top Left = {index} - {width} - 1
Top = {index} - {width}
Top Right = {index} - {width} + 1
...