Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm making a game in XNA and I want to limit the movement of a sprite to the screen boundaries. How would I do this?

share|improve this question

marked as duplicate by Byte56, Sean Middleditch, bummzack, Josh Petrie, Trevor Powell Feb 19 '13 at 1:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

The simplest way, if you just want the sprite to not go off the screen is this

if (spritePosition.X > Window.ClientBounds.Width - spriteTexture.Width)
     spritePosition.X = Widow.ClientBounds.Width - spriteTexture.Width;
if (spritePosition.Y > Window.ClientBounds.Height - spriteTexture.Height)
     spritePosition.Y = Widow.ClientBounds.Height - spriteTexture.Height;

if (spritePosition.X < 0)
    spritePosition.X = 0;
if (spritePosition.Y < 0)
    spritePosition.Y = 0; 
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.