vb.net 图片框之间的VB.NET碰撞
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15311255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET collision between pictureboxes
提问by user2151534
I'm trying to make a simple game and i need to know if picturebox1( my character) collides with other pictureboxes ( the walls).
我正在尝试制作一个简单的游戏,我需要知道picturebox1(我的角色)是否与其他pictureboxes(墙壁)发生碰撞。
I have already worked out how do this but it only works with my character and 1 other picturebox for example:
我已经弄清楚了如何做到这一点,但它仅适用于我的角色和其他 1 个图片框,例如:
If picturebox1.bounds.intersectWith(picturebox2.bounds) then
collision = true
end if
I tried to do something else like this:
我尝试做其他类似的事情:
For Each PictureBox In Me.Controls
If PictureBox1.Bounds.IntersectsWith(PictureBox.Bounds) Then
collision = True
Else : collision = False
End If
Next
But then the boolean collision would always be true because picturebox1 (the character) always intersects with itself.
但是布尔冲突将始终为真,因为 picturebox1(字符)始终与自身相交。
So i changed the picturebox into a panel and the code looks the following:
所以我将图片框更改为面板,代码如下所示:
For Each PictureBox In Me.Controls
If Panel1.Bounds.IntersectsWith(PictureBox.Bounds) Then
collision = True
Else : collision = False
End If
Next
But it only works with 1 single picture box and not with all the pictureboxes in the form. I don't understand why... And if anyone maybe knows how to add an exception in the for each function so i can keep my picturebox1
但它仅适用于 1 个图片框,而不适用于表单中的所有图片框。我不明白为什么......如果有人可能知道如何在每个函数中添加一个例外,这样我就可以保留我的图片框1
something like this maybe
像这样的事情也许
For each picturebox(except(picturebox1)) in me.controls
because i've searched for that but didn't find anything...
因为我已经搜索过但没有找到任何东西......
Any help is greatly appreciated :) Thanks!
非常感谢任何帮助:) 谢谢!
采纳答案by Abdusalam Ben Haj
One way of doing it:
一种方法:
For Each PictureBox In Me.Controls
If PictureBox IsNot PictureBox1 AndAlso PictureBox1.Bounds.IntersectsWith(PictureBox.Bounds) Then
collision = True
Exit For 'Exit when at least one collision found
Else : collision = False
End If
Next
This would set collision to False
if PictureBox is indeed PictureBox1. But note that you are overwriting the collision state in each loop, which not what you really want. You should exit the for loop when one collision is found (see my code). You may also change your code like this :
False
如果 PictureBox 确实是 PictureBox1,这将设置碰撞。但请注意,您正在覆盖每个循环中的碰撞状态,这不是您真正想要的。当发现一个碰撞时,您应该退出 for 循环(请参阅我的代码)。您也可以像这样更改代码:
collision = False
For Each PictureBox In Me.Controls
If PictureBox IsNot PictureBox1 AndAlso PictureBox1.Bounds.IntersectsWith(PictureBox.Bounds) Then
collision = True
Exit For
End If
Next