I'm writing a platform game in c++ and using a tiled map to build the world.
The map contains values which are assigned tiles:
eg of a 2D array:
int map[4][4]={
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}, };
I create a sprite in a similar way: int sprite[1][1]={ {2} };
My question is, how do you access a specific tile or element in a 2D array ?
I read this post(3rd post down) and wondering whether 2D arrays aren't the best to use.
My sprite always stays centred to the window and upon key presses my world moves behind him.
I want to collide with certain tiles, therefore need some interaction between my sprite 2D array tile and my Map 2D array...
Also as it stands, I can walk out of the map because I haven't coded it otherwise. How would I go about this ? Ideally I want to stop him leaving the map, then I can integrate the collision with that.
Below is a screenshot of what things look like(but with a bigger map than shown above):
If I have:
int array[4][4];
I can access elements by:
array[a_num][also_a_num]
where i and j are valid indices.
Usually when I want something like this I just use a 1d array and some math.
int height = a_num;
int width = also_a_num;
int amap[height * width];
int x_pos,y_pos;
int value = amap[y_pos*width + x_pos];
You can check bounds on x_pos and y_pos pretty easily.
As to the collision, you could try an array of structs.
struct tile{
bool walkable;
int type;
};
tile amap[height*width];