通过For / For Each循环对PictureBox进行碰撞检测

时间:2015-03-16 19:27:49

标签: arrays vb.net loops collision-detection picturebox

我正在创建一个简单的游戏作为Windows窗体应用程序,我正在尝试编写播放器的图片框和游戏中的墙壁之间的碰撞检测,这些也是图片框。我没有用IF语句对每一面墙进行编程,而是决定使用For循环。

我首先把图片框放到一个数组中:

Dim walls() As PictureBox = {wall1, wall2, wall3, wall4, wall5, wall6, wall7, wall8, wall9, wall10, wall11, wall12, wall13, wall14, wall15}

然后我将其作为碰撞检测循环(picCar是玩家):

        For x As Integer = 0 To 14 'I also tried this with "1 To 1"
        If picCar.Bounds.IntersectsWith(walls(x).Bounds) Then
            If lives > 1 Then
                lives -= 1
                movetostart()
            Else
                MsgBox("Game Over!")
            End If
        End If
    Next

这返回了“NullReferenceException未处理”错误,所以我在For Each循环中尝试了它:

        For Each wall As PictureBox In walls
        If picCar.Bounds.IntersectsWith(wall.Bounds) Then
            If lives > 1 Then
                lives -= 1
                movetostart()
            Else
                MsgBox("Game Over!")
            End If
        End If
    Next

这返回了同样的错误。我不太确定我做错了什么?我通过其他线程找到并测试了这两个想法,并且在这个问题上找不到其他的东西,你能帮忙吗?

线程链接:

Make Images intersect and gravity in VB

VB.NET collision between pictureboxes

0 个答案:

没有答案