I was just talking to Ruddie about loading in strings, and how strings can be accessed like arrays (for example, if "temp" is a string, then "temp[3]" will write out the 3rd character). It is a 1D array, but you can use Modulus to kind of "simulate" it being 2D.
Let's say we have a 1D array of tiles size 12. We want a 4x3 grid.
Modulus will essentially take anything bigger than the width (4), and offset it.
After it gets to index 3, it loops back and 4 begins at the first position on the next row.
I'm feeling really unarticulate today, so I apologize. XD Best way to show is this way...:
- Code: Select all
0 1 2 3 <-- Row 0: Goes up to the tile corresponding to the width (0 to 4)
4 5 6 7 <-- Row 1: Treats 4 like 4-width (4-4 = 0), but adds one to the row it's on.
8 9 10 11 <-- Row 2: Treats 8 like 8-(2*width) (8-(2*4) = (8-8 = 0)
So how do we get X and Y coordinates from a 1D array?
With width being 4, you can get the x and y coordinates this way:
x = index % width
y = index / width
So for tile 0:
0 % width = 0
0 / width = 0
coordinates are (0, 0)
For tile 1:
1 % width = 1
1 / width = 0
coordinates are (1, 0)
For tile 3:
3 % width = 3
3 / width = 0
coordinates are (3, 0)
For tile 4:
4 % width = 0
4 / width = 1
coordinates are (0, 1)
For tile 7:
7 % width = 3
7 / width = 1
coordinates are (3, 1)
For tile 9:
9 % width = 1
9 / width = 2
coordinates are (1, 2)
Sorry I'm not feeling very articulate today, I'm having a hard time explaining mod. Maybe I'll edit this later.
