Time for action – repositioning the camera
Add the
Helper Methods
region and therepositionCamera()
method to thePlayer
class:#Region "Helper Methods" Private Sub repositionCamera() Dim screenLocX As Integer screenLocX = CInt(Int(Camera.WorldToScreen(worldLocation).X)) If screenLocX > 500 Then Camera.Move(New Vector2(screenLocX - 500, 0)) End If If screenLocX < 200 Then Camera.Move(New Vector2(screenLocX - 200, 0)) End If End Sub #End Region
In the
Update()
method of thePlayer
class, add a call to reposition the camera right before the call toMyBase.Update()
:repositionCamera()
Execute the Gemstone Hunter application and move towards the right side of the screen.
What just happened?
During each update frame, the current screen position of the character is checked. If the character has gotten too close to the edge of the screen (200 pixels from the left edge, or 300 pixels from the right edge), the camera's position is adjusted to keep the character...