简单测试中的碰撞检测失败 "game"
Failed collision detection in simple test "game"
我已经重做了几次,但我无法让它工作...当 button1 位于 button2 的下方 and/or 最右侧时,它会检测到碰撞,但如果不是它在上部 and/or 最左边的部分...很高兴知道问题出在哪里因为我不擅长调试...
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))
)
)
看来问题出在这里:
(button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
这大概是在尝试查看 button1
的底部是否在 button2
内,因此它应该与 button2.Top
和 button2.Top + button2.Height
进行比较。
button1.Right
可能有类似的问题。
我这样做了,效果很好。它基本上只是检查左上角是否在另一个按钮的位置。棘手的部分只是在第二次比较中添加宽度和高度,这实际上使 button1 偏移了大小,因此如果它在 button2
中,则位置将大于 button2 的位置
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");
这将进行您尝试进行的比较。
我已经重做了几次,但我无法让它工作...当 button1 位于 button2 的下方 and/or 最右侧时,它会检测到碰撞,但如果不是它在上部 and/or 最左边的部分...很高兴知道问题出在哪里因为我不擅长调试...
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))
)
)
看来问题出在这里:
(button1.Bottom >= button2.Bottom && button1.Bottom <= (button2.Bottom + button2.Height))
这大概是在尝试查看 button1
的底部是否在 button2
内,因此它应该与 button2.Top
和 button2.Top + button2.Height
进行比较。
button1.Right
可能有类似的问题。
我这样做了,效果很好。它基本上只是检查左上角是否在另一个按钮的位置。棘手的部分只是在第二次比较中添加宽度和高度,这实际上使 button1 偏移了大小,因此如果它在 button2
中,则位置将大于 button2 的位置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");
这将进行您尝试进行的比较。