[.net] XNA sprite clicking

Started by
4 comments, last by Emmanuel Deloget 16 years, 9 months ago
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.
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
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."
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
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,
This is a simular to the way that I do it in the example I posted above.
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
Quote:Original post by Mykre
This is a simular to the way that I do it in the example I posted above.


Sorry, I haven't checked when I posted my answer [smile]

This topic is closed to new replies.

Advertisement