Tile Movement Question

Started by
1 comment, last by MrCpaw 15 years, 3 months ago
I'm running into a bit of a problem. I have setup a tile map and I've drawn it out to the screen fine. Then I made an exact duplicate of that map but to check for walls, items, ect... Here is my movement map:

int Map_One_Movement[15][20] = {
	{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
        {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}
};



0 equals movable and 1 equals Wall Tile which you cannot move. Now my problem is here:

                if (key[KEY_UP] && Map_One_Movement[Player_Y - 32][Player_X] == 0)
		{
			allegro_message("Moving Up!");

			Player_Y -= 32;
		}
		else if (key[KEY_UP] && Map_One_Movement[Player_Y - 32][Player_X] == 1)
		{
			allegro_message("Cannot Move!");
		}

		else if (key[KEY_DOWN] && Map_One_Movement[Player_Y + 32][Player_X] == 0)
		{
			allegro_message("Moving DOWN!");
			
			Player_Y += 32;
		}
		else if (key[KEY_DOWN] && Map_One_Movement[Player_Y + 32][Player_X] == 1)
		{
			allegro_message("Cannot Move!");
		}

		else if (key[KEY_LEFT] && Map_One_Movement[Player_Y][Player_X - 32] == 0)
		{
			allegro_message("Moving LEFT!");

			Player_X -= 32;
		}
		else if (key[KEY_LEFT] && Map_One_Movement[Player_Y][Player_X - 32] == 1)
		{
			allegro_message("Cannot Move!");
		}

		else if (key[KEY_RIGHT] && Map_One_Movement[Player_Y][Player_X + 32] == 0)
		{
			allegro_message("Moving RIGHT!");

			Player_X += 32;
		}
		else if (key[KEY_RIGHT] && Map_One_Movement[Player_Y][Player_X + 32] == 1)
		{
			allegro_message("Cannot Move!");
		}

What I'm trying to do is check to see if that part in the Array is holding 0 or 1 and if it's holding 0 allow movement, if 1 do nothing. NOTE: I just put allegro_message(""); there so I would know if everything working and what's being done.
Advertisement
The problem here is that you're trying to access the tile data using screen x and y positions.

You should divide the Player_X and Player_Y with the width/height of one tile (looking at your code hints that they're 32x32) to convert the screen positions to tile positions.
My bad, I just noticed the problem, I forgot to / 32 to Player_X and Player_Y in the Map_One_Movement Array.

I cannot believe I missed that. Now I think I can crown myself a "noob".

This topic is closed to new replies.

Advertisement