Reputation: 569
How do I make a scroll-able map. For example, my preferred back buffer is 800x600 and the map is 2400x1800 (approximately 3x3). Also, how do I handle the keyboard state to scroll and do walking. I know most games keep the player centered and scroll the world. The problem with this approach is the corners. There would be a large unmoveable area.
Upvotes: 0
Views: 337
Reputation: 86
To make scrollable map you can use simple Rectangle or ViewPort (named camera):
' Initialize camera with size of game viewport
Dim viewport As Viewport = spriteBatch.GraphicsDevice.Viewport
Dim camera As New Rectangle(viewport.X, viewport.Y, viewport.Width, viewport.Height)
' Draw method code
spriteBatch.Begin()
spriteBatch.Draw(image,
New Rectangle(0, 0, viewport.Width, viewport.Height), // Destination rectangle
camera, // Source rectangle
Color.White)
spriteBatch.End()
By changing camera.X and camera.Y values you can adjust origin from where your image is drawn thus moving camera around. For example following code would move your camera to the right:
Dim keyboardState As KeyboardState = Keyboard.GetState()
If keyboardState.IsKeyDown(Keys.Right) Then
camera.X += 1
End If
Walking can be done very similarly, by increasing character position X and Y coordinates when according buttons are pressed.
Keeping player in the center of the screen is a bit trickier. Basically you want to keep character in the center of the screen at all times except when distance between it and edge of world is less then half the screen. In this case stop moving camera and start moving character off of center to the desired side.
Upvotes: 1