# Background Scrolling like Zelda - A Link To The Past with XNA [duplicate]

i am trying to create a background that scrolls like the one in zelda - a link to the past. in other words: i have a bigger "screen" than the camera covers, so i need to scroll over it, when moving. if the player reaches an edge of the screen, the camera is going to stop and the character moves out of the center.

my actual problem is, that i have to draw the objects in an other way than the background. i am getting the camera to track the player, but if i want to add another object like an enemy, i have to draw it with screen coordinates, which are not the same as the background coordinates. (background image is 1280x720 with a source rectangle of 640x360). so the player is usually drawn at 640/2, 360/2. but the player also is at position 600,600 of the background image. so, if there is an enemy at 200,200 and the player moves to that point i want to draw the enemy on the screen. but, of course, i cant draw it at 200,200 in the screen that displays the sorucerectangle of the texture.

so i am looking for a way to kinda get the object into the texture, that there is only one coordinate system or at least a second coordinate system where all the object are in., which is as big as the texture itsself. with such a coordinate system i could get the absolute positions of the objects and draw it like the sourcerectanlge of a texture.

## marked as duplicate by Josh♦Aug 13 '15 at 15:17

• I don't know about xna but think you can calculate borders of your view and than check if your camera will get out of the borders else move the camera. – Arda Kara Aug 12 '15 at 21:33
• my problem is that i did not use a camera in the project yet. i keep the player drawn in the center and do something like moving the background. i think this is my mistake. i thought of a camera-class, but i created the thread to look, if theres an easier solution. i think i am gonna try the camera tomorrow – purpule Aug 12 '15 at 21:44
• Then you can calculate distance between player and backgrounds border. If the difference is smaller than players distance to screen edge move the player instead background. – Arda Kara Aug 12 '15 at 21:46

So basically what you want to do is check to see what the scroll position is after a key is pressed, and with an if else statement, check to see whether you should move the background or the character. For example:

    if (CurrentKeyboardState.IsKeyDown(keys.Right))
{
if (BackgroundXPosScroll < BackgroundTexture.Width - CurrentScreenLength - 5)
{
BackgroundXPosScroll += 5;
}
else
{
CharacterXPosScroll += 5;
}
}
BackgroundXPosition = -BackgroundXPosScroll;
CharacterXPosition = CharacterXPosScroll + CharacterXPositionOnScreen;


Then you want to check with the left arrow key if the character X position is bigger than the center of the screen... if so, move the character, if not, change the BackgroundXPosScroll by -5. That takes care of moving one direction... the other directions are just different implementations of that code.