Tôi đang tạo ra một trò chơi sinh tồn và cố gắng loại bỏ một vật khi nó tắt màn hình. Đây là mã:Xóa vật thể khi ở trong mỗi vòng
Public Sub tmrEnemyMove_Tick(sender As Object, e As EventArgs) Handles tmrEnemyMove.Tick
Dim koopaAnimation As Boolean
For Each enemy As enemy In lstEnemy
enemy.enemy.Left = enemy.enemy.Left - 20
If enemy.enemy.Tag = "koopa" Then
enemy.enemy.Image = Image.FromFile(Application.StartupPath + "\Graphics\koopa" + Trim(Str(koopaPosition)) + ".png")
If koopaAnimation = False Then
If koopaPosition = 0 Then
koopaPosition = 1
Else
koopaPosition = 0
End If
End If
koopaAnimation = True
End If
If picMario.Left < enemy.enemy.Left AndAlso enemy.enemy.Left < picMario.Right Or picMario.Left < enemy.enemy.Right AndAlso enemy.enemy.Right < picMario.Right Then
If picMario.Top < enemy.enemy.Top AndAlso enemy.enemy.Top < picMario.Bottom Or picMario.Top < enemy.enemy.Bottom AndAlso enemy.enemy.Bottom < picMario.Bottom Then
'MsgBox("Collision")
End If
End If
If enemy.enemy.Left < 0 Then
lstEnemy.Remove(enemy)
Me.Controls.Remove(enemy.enemy)
End If
Next
End Sub
Các lỗi tôi nhận được là: Một ngoại lệ unhandled của loại 'System.InvalidOperationException' xảy ra ở mscorlib.dll Thông tin thêm: Bộ sưu tập đã được sửa đổi; hoạt động điều tra có thể không thực hiện được.
Nếu có ai đó có thể giúp điều đó thật tuyệt vời, cảm ơn.
Bạn canot thay đổi bộ sưu tập như trong khi điều này lặp qua nó , vì những lý do hiển nhiên nếu bạn nghĩ về nó. Nhận một tham chiếu đến đối tượng trong khi ở vòng lặp, sau đó xóa aterwards – DavidB