Jump to content
  • Advertisement
Sign in to follow this  
MrCpaw

Tile Movement Question

This topic is 3537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!