图片框之间的VB.NET冲突 [英] VB.NET collision between pictureboxes

查看:69
本文介绍了图片框之间的VB.NET冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的游戏,我需要知道picturebox1(我的角色)是否与其他Picturebox(墙壁)相撞.

I'm trying to make a simple game and i need to know if picturebox1( my character) collides with other pictureboxes ( the walls).

我已经知道了如何做到这一点,但是它仅适用于我的角色和另外1个图片框:

I have already worked out how do this but it only works with my character and 1 other picturebox for example:

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

但是布尔值冲突永远是正确的,因为picturebox1(字符)总是与其自身相交.

But then the boolean collision would always be true because picturebox1 (the character) always intersects with itself.

因此我将图片框更改为一个面板,代码如下所示:

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

但是它仅适用于1个单个图片框,而不适用于表单中的所有图片框. 我不明白为什么... 如果有人知道如何为每个函数添加一个例外,这样我就可以保留我的picturebox1

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

也许是这样

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!

推荐答案

一种方法:

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

如果PictureBox确实是PictureBox1,这会将碰撞设置为False.但是请注意,您正在覆盖每个循环中的碰撞状态,而这并不是您真正想要的.当发现一个冲突时,您应该退出for循环(请参阅我的代码).您也可以像这样更改代码:

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 :

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

这篇关于图片框之间的VB.NET冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆