Time for action – staying in bounds
Create a region called
Movement
Limitations
in thePlayer
module:#Region "Movement Limitations" #End Region
Inside the
Movement Limitations
region, add theclampToWorld()
method:Private Sub clampToWorld() Dim currentX As Single = BaseSprite.WorldLocation.X Dim currentY As Single = BaseSprite.WorldLocation.Y currentX = MathHelper.Clamp( currentX, 0, Camera.WorldRectangle.Right - BaseSprite.FrameWidth) currentY = MathHelper.Clamp( currentY, 0, Camera.WorldRectangle.Bottom - BaseSprite.FrameHeight) BaseSprite.WorldLocation = New Vector2(currentX, currentY) End Sub
Add a declaration to the
Player
module, to define the area in which the camera should attempt to keep the player:Private scrollArea As Rectangle = New Rectangle(150, 100, 500, 400)
Add the
repositionCamera()
helper method to theMovement
Limitations
region of thePlayer
module:Private Sub repositionCamera( gameTime As GameTime, moveAngle As Vector2) ...