Skip to main content
gamedev.net
🔒 Locked

Scrolling

Started by Hannibal111111 Jul 21, 2006 at 3:05 PM 1 replies 1.4k views
Original Post
Hannibal111111
Hannibal111111
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[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
Rasmadrak
Rasmadrak
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!
"Game Maker For Life, probably never professional thou." =)
furiousuk
furiousuk
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.