Rectangle.IntersetsWith()每当两个矩形是在同一个X轴返回true(Recta

2019-10-20 03:15发布

我试图重新战舰和我使用Rectangle.IntersectsWith()找到每艘船舶之间的碰撞。 每当我把船只在同一x轴的网格,该函数返回true。 它没有,如果他们是在相同的Y轴或者如果他们是感人。

这是我用它来测试针对每艘其他船舶每艘船舶(如果你知道一个更简单的方法,我很愿意听到的话)的代码。

Public Sub CheckForCollision()
    Dim ships As Ship() = _
        {AirCraftCarrier, Battleship, Submarine, _
            Destroyer, PatrolBoat}

    For i As Integer = 0 To 4
        Dim ship1 As Ship = ships(i)
        For j As Integer = 0 To 4
            Dim ship2 As Ship = ships(j)
            If ship1.name <> ship2.name Then
                If ship1.rect.IntersectsWith(ship2.rect) Then
                    Debug.Print(ship1.name & " and " & ship2.name & " intersect")
                End If

            End If
        Next
    Next
End Sub

每当船舶移动到一个新的斑点rects x和y值的变化。 我只是将它设置为图片框的位置。

Answer 1:

用于测试在特定船舶科里森:

Public Function SoundCollision(thisShip As Ship) as Boolean
    Dim ships As Ship() = _
        {AirCraftCarrier, Battleship, Submarine, _
            Destroyer, PatrolBoat}

    Dim bRet as Boolean = False          ' assume not

    For n As Integer = 0 To 4
        If ships(n).Name <> thisShip.Name Then
            If thisShip.rect.IntersectsWith(ships(n).rect) Then
                bret = true
                Debug.Print(ships(n).name & " intersects " & thisShip.name)
                Exit For            ' skip the rest
            End If

        End If
    Next
    Return bRet
End Sub

如果是在你船级方法,利用我,而不是thisShip的和不传递



文章来源: Rectangle.IntersetsWith() returning true whenever the two rectangles are on the same X axis