Jump to content
  • Advertisement
Sign in to follow this  
Montynis

[.net] XNA sprite clicking

This topic is 4085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have an XNA app where some sprites are loaded and drawn. I am trying to find out is there a possibility to detect that the user has clicked a sprite (with left mouse button)? One way is to use this solution (with some little changes): http://www.xnaresources.com/pages.asp?pageid=25 Is there any other solutions? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Most of the time you detect the sprite that was clicked by the location, not by color. How you do it depends on how many sprites you are dealing with. If not very many, then the brute force method works quiet well. With a lot of sprites then it gets a bit more complicated and you need to cut down the number of sprites you are searching through.

theTroll

Share this post


Link to post
Share on other sites
I have put together a small example that you might like to look at...

XNA Example - Clickable Sprite

"In this Example I show you a simple method to use the Mouse (Using my Custom Mouse Pointer Game Component) to click on a Sprite. This example also uses a Per Pixel Method that will make sure that you can only click on the full sprite and not on a Transperant area."

Share this post


Link to post
Share on other sites
Quote:
Original post by Montynis
Hello,

I have an XNA app where some sprites are loaded and drawn.

I am trying to find out is there a possibility to detect that the user has clicked a sprite (with left mouse button)?

One way is to use this solution (with some little changes):

http://www.xnaresources.com/pages.asp?pageid=25

Is there any other solutions?

Thanks in advance.


You are supposed to know where the sprite is, don't you? Given the fact that you specify it's position when you call SpriteBatch.Draw() and that the sprite size shall be known too (if you're using a complete texture to represent a sprite (something which shall be done only if you have one sprite to draw [smile]), you can get its size from the Width/Height properties of that texture. If you are using sprite sheets, you shall know the size of the sprite you just drawn, since you specified the source rect when you called the ubiquitous SpriteBatch.Draw()).

Once you know the sprite destination rectangle, you just have to see if the mouse is actually in this rectangle when the click happen.

If this is not precise enough, you can check the mouse position against a collision map (or the alpha map if your sprite has an alpha map). Cheking against a collision map is quite simple - load the map as a bitmap, and extract the color of the clicked point, Chakcing against the alpha map might require a texture read.

HTH,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!