简体   繁体   English

C#XNA Windows Phone 7游戏:如何知道按下了哪些对象

[英]C# XNA Windows Phone 7 game: how to know which objects are pressed

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. 假设我有多个对象,如果单击其中一个,则从TouchPanel.GetState()返回的坐标将无法告诉我选择了哪个对象。 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. 在这种情况下,我是否组织对象列表,以便按Z值排序并从上至下比较矩形。 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? 还是是否有UI框架提供某种要继承的组件,以便在发生外部事件时对象将具有回调?

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游戏库,则必须使用Z值来确定选择了哪个对象。

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. XNA的TouchPanel.GetState()只会返回手指在屏幕上的位置,它不会告诉您它与哪个对象发生碰撞。 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. 您必须自己进行所有碰撞检查,在这里可以通过Z或分层顺序决定要选择的对象。

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. 对于2D对象,没有应用任何特定的Z顺序(因为它们是2D,而不是3D),因此您需要添加一个属性来处理该对象的类。

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. 然后,当您触摸某个位置时,您可以收集该位置与之碰撞的所有对象,并根据z顺序选择所需的对象。

Hope that helps! 希望有帮助!

This XNA sample should get you started: 此XNA示例应帮助您入门:

http://create.msdn.com/en-US/education/catalog/sample/picking http://create.msdn.com/en-US/education/catalog/sample/picking

As Nick Funk said, you have the 2D touch coordinates, then it's just a matter of applying the above sample to your game and using those coordinates. 正如尼克·芬克(Nick Funk)所说,您拥有2D触摸坐标,只需将上述示例应用于游戏并使用这些坐标即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM