C# 简单测试中未检测到碰撞”;游戏“;
我已经重新做了几次,但我不能让它工作。。。当button1位于button2的最下方和/或最右侧时,它会检测碰撞,但如果button1位于最上方和/或最左侧,则不会检测碰撞。。。很高兴知道问题出在哪里,因为我在调试方面很差劲C# 简单测试中未检测到碰撞”;游戏“;,c#,2d,C#,2d,我已经重新做了几次,但我不能让它工作。。。当button1位于button2的最下方和/或最右侧时,它会检测碰撞,但如果button1位于最上方和/或最左侧,则不会检测碰撞。。。很高兴知道问题出在哪里,因为我在调试方面很差劲 if ( ( (button1.Top >= button2.Top && button1.Top <= (button2.Top + button2.Height)) || (button1.Botto
if (
(
(button1.Top >= button2.Top && button1.Top <= (button2.Top + button2.Height))
|| (button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
)
&&
(
(button1.Left >= button2.Left && button1.Left <= (button2.Left + button2.Width))
|| (button1.Right >= button2.Right && button1.Right <= (button2.Right + button2.Width))
)
)
if(
(
(button1.Top>=button2.Top&&button1.Top=button2.Bottom&&button1.Bottom=button2.Left&&button1.Left=button2.Right&&button1.Right问题似乎出在这里:
(button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
(button1.Bottom>=button2.Bottom&&button1.Bottom我这样做了,效果很好。它基本上只是检查左上角是否在另一个按钮的位置。棘手的部分是在第二次比较中添加宽度和高度,这实际上是通过大小来偏移按钮1,因此位置会很好如果在按钮2中,则er大于按钮2
if ((button1.Location.X > button2.Location.X && button1.Location.Y > button2.Location.Y)
||(button1.Location.X + button1.Size.Width > button2.Location.X
&& button1.Location.Y + button1.Size.Height > button2.Location.Y))
MessageBox.Show("In side other button");
然而,如果你想用一种更简单的方法来做,你可以这样做
if(button1.Bounds.IntersectsWith(button2.Bounds))
MessageBox.Show("Within button");
这将完成您试图进行的比较。拥有您喜欢的代码是可以的,但是如果您寻求帮助,让其他人更容易阅读是有意义的。谢谢!我实际上不知道这是一种合法的编写方式,但当我想到它时,它是非常明显的…:t谢谢!一个问题是,上面的l窗口0、0或左下角的eft像素?:D再次感谢!:DWow…如果我从一开始就知道,我现在已经完成了…我想我下次应该开始更近距离地寻找答案,而不是无缘无故地把它弄得太复杂了…:D非常感谢!:我明白你的意思,但我想。Top是从顶部到顶部的距离。底部是距离底部的距离,所以我认为将它们相互比较是行不通的……但我可能错了