Sign in to follow this  

This topic is 4146 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 know this has been asked and answered a hundred times, but I can't seem to get it to work for me. I'm trying to implement a scrollable tilemap but when I move right, the map seems to repeat instead of move the character to the right edge of the screen as I get closer to the right edge of the tilemap. Here is my map-rendering code:

void drawMap()
{
	for(int i=0;i<CAMERA_HEIGHT;i++) 
	{
		for(int j=0;j<CAMERA_WIDTH;j++) 
		{
			//drawIMG(surMap[i][j],(j * TILE_SIZE),(i * TILE_SIZE) + MAP_OFFSET);
			drawIMG(surMap[i + Hero.mapy][j + Hero.mapx],(j * TILE_SIZE),(i * TILE_SIZE) + MAP_OFFSET);
		}
	}
}


Hero.mapy and Hero.mapx just keeps track of where the character is in relation to the entire map. For example if character starts at 0,0 and I move right, mapx becomes 1. If I move down mapy becomes 1. If I move back up, mapy becomes 0. Perhaps this is not the way I should be approaching the problem but this is what I gleaned from some posts and articles here on Gamedev. Can anyone suggest where I might be messing up? Thanks, Han

Share this post


Link to post
Share on other sites
Probably your i and/or j becomes larger than the array that keeps the map, so it simply continues with element 0,1,2 in the array.

Make sure that i and j are within the map-range!

Share this post


Link to post
Share on other sites
The camera has a position and a size and this dictates which part of the map you draw. Think of the camera position coords as the top left tile to be displayed and the size is your area to display.

When a movement request is logged by your input routine two things happen - the hero always moves (unless a collision is detected) and the camera will either move or not -> This is the important bit. The camera will move if it is still within the map e.g. camerapos > 0 (assuming the map array begins at 0) and camerapos + camerasize < maparraysize. When handling a movement request the check for moving the camera is performed, this ensures the camera is always in bounds.

When rendering the scene draw the map from camerapos to camerapos + camerasize. You can perform a check to make sure you arent trying to access outside of the map array altho this isnt necessary as the camera would not have moved if this had occurred (in other words, your bounding checks have already been performed in the movement routine).

The key here is to seperate the hero and camera objects and have the movement code control both.

Share this post


Link to post
Share on other sites

This topic is 4146 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this