我无法找到此Stack Overflow异常的原因

时间:2014-05-16 03:09:29

标签: vb.net stack-overflow

自从我添加CheckForCollision_Enemy方法以来,每次运行代码时都会遇到堆栈溢出异常。 以下是CheckForCollision_Enemy类中的Main方法。

 Public Sub CheckForCollision_Enemy()
    Dim ship1 As New Enemy(Nothing, Nothing)
    Dim ship2 As New Enemy(Nothing, Nothing)
    Debug.Print("")

    Dim ships = {acc_e, bs_e, sb_e, ds_e, pb_e}

    For i As Integer = 0 To ships.Length - 1
        ship1 = ships(i)
        For j As Integer = 0 To ships.Length - 1
            ship2 = ships(j)
            If ship1.name <> ship2.name Then
                For l As Integer = 0 To ship1.length - 1
                    For t As Integer = 0 To ship2.length - 1
                        If ship1.space_filled(l, 0) = ship2.space_filled(t, 0) And ship1.space_filled(l, 1) = ship2.space_filled(t, 1) Then
                            Debug.Print("Collision at {" & ship1.space_filled(l, 0) & ", " & ship1.space_filled(l, 1) & "} " & ship1.name & " VS " & ship2.name)
                        End If
                    Next
                Next
            End If
        Next
    Next


End Sub

这是敌人类。这是显示错误的类。我准确地标记了评论的位置。

Shared gen As New Random()
Dim x As Integer = 0
Dim y As Integer = 0
Public Sub New(ByVal namep As String, ByVal lengthp As Integer)
    name = namep
    length = lengthp

    ReDim _start_point(2)
    ReDim _space_filled(length, 2)

    GenerateDirection()
    If direction = "horizontal" Then
        x = gen.Next(0, 11 - length)
        y = gen.Next(0, 10)
    ElseIf direction = "vertical" Then
        x = gen.Next(0, 10)
        y = gen.Next(0, 11 - length)
    End If
    GenerateStartPoint()
    ExtendStartPoint()

    DefineFilled()
    ColorFilled()

    Main.CheckForCollision_Enemy() 'If this is taken out, it will work fine.
End Sub

    Public Sub GenerateStartPoint()
    start_point = {x, y}
End Sub
Public Sub GenerateDirection()
    If gen.Next(0, 2) = 0 Then
        direction = "horizontal"
    Else
        direction = "vertical"
    End If
End Sub

Public Sub ExtendStartPoint()
    If direction = "horizontal" Then
        For i As Integer = 0 To length - 1
            space_filled(i, 0) = start_point(0) + i
            space_filled(i, 1) = start_point(1)
        Next
    ElseIf direction = "vertical" Then
        For i As Integer = 0 To length - 1
            space_filled(i, 0) = start_point(0)
            space_filled(i, 1) = start_point(1) + i
        Next
    End If
End Sub

Public Sub DefineFilled()
    For i As Integer = 0 To length - 1
        x = space_filled(i, 0)
        y = space_filled(i, 1)
        Try
            generate = False
            Main.TrackerBoard.box_list(x, y).full = True
        Catch
        End Try
    Next
End Sub
Private Sub ColorFilled()
    For y As Integer = 0 To 9
        For x As Integer = 0 To 9
            'Debug.Print(Main.PlayerBoard.box_list(x, y).full)
            If Main.TrackerBoard.box_list(x, y).full = True Then 'New error: "InvalidOperationException"
                Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Red
            Else
                Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Silver 'Most often, the error appears here.


            End If
        Next
    Next
End Sub

这是船级。我已经拿出了大部分方法来节省空间;如果你想看到任何东西,我会为你添加它。

Public Class Ship
    Dim _name As String
    Public Property name() As String ...

    Dim WithEvents _image As PictureBox
    Public Property image() As PictureBox ...

    Dim _length As Integer
    Public Property length() As Integer ...

    Dim _direction As String
    Public Property direction() As String ...

    Dim _selected As Boolean
    Public Property selected() As Boolean ...

    Dim _placed As Boolean
    Public Property placed() As Boolean ...

    Dim _location() As Integer = {0, 0}
    Public Property location() As Integer() ...

    Dim _has_moved As Boolean
    Public Property has_moved() As Boolean ...

    Dim _space_filled(,) As Integer
    Public Property space_filled() As Integer(,) ...

    Public rect As System.Drawing.Rectangle

    Dim mouse_up As Boolean = False
    Dim tile_size As Integer = 25

    Public Sub New(ByVal namep As String, ByVal imagep As PictureBox, ByVal lengthp As Integer, ByVal directionp As String, ByVal selectedp As Boolean, ByVal placedp As Boolean)
        name = namep
        image = imagep
        length = lengthp
        direction = directionp
        selected = selectedp
        placed = placedp

        location(0) = 0
        location(1) = 0

        ReDim space_filled(length, 2)
        rect = New System.Drawing.Rectangle(location(0), location(1), length * tile_size, 1 * tile_size)

    End Sub

    Private Sub Ship_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseMove ...

    Private Sub Ships_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseClick ...

    Private Sub Ship_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseUp ...


    Public Sub Update() ...


    Public Sub SnapToBox() ...


    Private Sub DefineSpaceFilled() ...


    Private Sub ColorFilled() ...

End Class

1 个答案:

答案 0 :(得分:3)

您的CheckForCollision_Enemy来电New EnemyNew Enemy来电CheckForCollision_Enemy。你正在递归,直到堆栈溢出。