Say I have multiple objects laying on each other, if I click on one of them, the coordinates returned from TouchPanel.GetState() would not able to tell me which object is selected. The object is not necessary the top most one. In this case, do I organize the list of objects so that sorted by Z value and compare Rectangles from top to bottom. Is this right way to do that? Or if there is any UI framework providing some sort of component to inherit so that the object will have callback when there is external event happening?
Share
If you’re using the XNA Game Libraries, you’re going to have to use a Z value to determine which object was selected.
XNA’s TouchPanel.GetState() will only return the position of the fingers on the screen, it won’t tell you which objects it’s colliding with. You have to do all that collision checking yourself, which is where you can decide via Z or layering order, which object you’d like to select.
For 2D objects, there isn’t any specific Z order applied to them (because they’re 2D, not 3D), so you’ll need to add a property to handle that to your object’s class.
Then when you touch a position, you can collect all of the objects that the position collided with and select whichever object you want based on it’s z order.
Hope that helps!