Arrays Rectangle.InternetsWith()在两个矩形位于同一X轴上时返回true

Arrays Rectangle.InternetsWith()在两个矩形位于同一X轴上时返回true,arrays,vb.net,loops,geometry,collision-detection,Arrays,Vb.net,Loops,Geometry,Collision Detection,我正在尝试重建战舰,我正在使用Rectangle.IntersectsWith()查找每艘战舰之间的碰撞。每当我把船放在网格上相同的x轴上时,函数返回true。如果它们在同一个y轴上,或者它们相互接触,则不会发生任何变化 这是我用来测试每艘飞船和其他飞船的代码(如果你知道一种更简单的方法,我很想听听) Public Sub-CheckForCollision() 将船舶设置为船舶()_ {航空母舰、战列舰、潜艇、_ 驱逐舰,巡逻艇} 对于i,整数=0到4 将ship1的尺寸标注为Ship=Shi

我正在尝试重建战舰,我正在使用Rectangle.IntersectsWith()查找每艘战舰之间的碰撞。每当我把船放在网格上相同的x轴上时,函数返回true。如果它们在同一个y轴上,或者它们相互接触,则不会发生任何变化

这是我用来测试每艘飞船和其他飞船的代码(如果你知道一种更简单的方法,我很想听听)

Public Sub-CheckForCollision()
将船舶设置为船舶()_
{航空母舰、战列舰、潜艇、_
驱逐舰,巡逻艇}
对于i,整数=0到4
将ship1的尺寸标注为Ship=Ship(i)
对于j,作为整数=0到4
尺寸ship2为Ship=Ship(j)
如果ship1.name ship2.name,则
如果ship1.rect.与(ship2.rect)相交,则
Debug.Print(ship1.name&“和”&ship2.name&“相交”)
如果结束
如果结束
下一个
下一个
端接头

每当船舶移动到新地点时,矩形x和y值都会更改。我只是将其设置为PictureBox位置。

在特定船舶上测试collison:

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
公共函数SoundCollision(此船为船)为布尔值
将船舶设置为船舶()_
{航空母舰、战列舰、潜艇、_
驱逐舰,巡逻艇}
Dim bRet as Boolean=False'假定不存在
对于n,作为整数=0到4
如果船(n)。命名这艘船。然后命名
如果此ship.rect.与(ships(n.rect))相交,则
布雷特=真
Debug.Print(ships(n.name)&“intersects”&thiship.name)
退出“跳过其余部分”
如果结束
如果结束
下一个
回程布雷特
端接头

如果这是飞船类中的方法,请使用我而不是这艘船,不要传递它

这些家伙如何移动,鼠标拖动?您存储的X,Y可能是错误的引用。X,Y(点)可以相对于窗体、屏幕或图片框,具体取决于您获取它们的方式。表格(10,20)图片盒(10,20)。
rects
是如何初始化的,如果它们有错误的H或W,它也会报告错误。鼠标将其拖动到屏幕上,然后左上角被设置到它所在的框的左上角。啊,这就是问题所在!我把H和W弄混了。再次谢谢!我还有一个问题,也许你能帮忙
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