Time for action – staying in bounds
Create a region called "Movement Limitations" in the Player class:
#region Movement Limitations #endregion
Inside the Movement Limitations region, add the
clampToWorld()
method:private static void clampToWorld() { float currentX = BaseSprite.WorldLocation.X; float currentY = 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); }
Add a declaration to the Player class to define the area in which the camera should attempt to keep the player:
private static Rectangle scrollArea = new Rectangle(150, 100, 500, 400);
Add the
repositionCamera()
helper method to the Movement Limitations region of the Player class:private static void repositionCamera( GameTime gameTime...