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

If I use mouse coordinates to select a sprite does XNA provide a built in way to return the top-most sprite on the screen at a coordinate?

I am trying to highlight the selected sprite, but when iterating through my sprites at the specific mouse location there can be multiple sprites found. The way my game works any one of the sprites found is valid to be on top.

share|improve this question
up vote 2 down vote accepted

I don't believe XNA has a built in way to do this. You should definitely implement some kind of ordering or layering system so one sprite gains precedence over the others (unless your game needs multiple sprites to be selected at once from the same mouse click). Once a sprite processes the selection no other sprites should be given the chance to.

share|improve this answer
I headed down this path and it will solve my issue. – riv_rec Apr 5 '11 at 15:59

It sounds like you need to assign a "depth" or "Z" value to your sprites that defines which is "on top" of each other. Then you would iterate through the sprites as usual but the sprite returned would have the highest "Z" value.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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