我正在尝试为具有网格和刻度计数器的游戏制作一些代码,但网格只是尝试填充窗口而不是停止并且不显示刻度计数器。不断出现的错误是:
类型' System.ObjectDisposedException'的第一次机会异常。发生在System.Windows.Forms.dll
中
我不知道这意味着什么,我也不知道如何解决它。
继承我的代码:
Public Class Form1
Dim G As Graphics
Dim BBG As Graphics
Dim BB As Bitmap
Dim r As Rectangle
Dim tSec As Integer = TimeOfDay.Second
Dim tTicks As Integer = 0
Dim MaxTicks As Integer = 0
Dim IsRunning As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Focus()
G = Me.CreateGraphics
BB = New Bitmap(Me.Width, Me.Height)
StartGameLoop()
End Sub
Private Sub DrawGraphics()
For X = 0 To 19
For Y = 0 To 14
r = New Rectangle(X * 32, Y * 32, 32, 32)
G.FillRectangle(Brushes.BurlyWood, r)
G.DrawRectangle(Pens.Black, r)
Next
Next
G.DrawString("Ticks: " & tTicks & vbCrLf & _
"TPS: " & MaxTicks, Me.Font, Brushes.Black, 650, 0)
G = Graphics.FromImage(BB)
BBG = Me.CreateGraphics
BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)
G.Clear(Color.Wheat)
End Sub
Private Sub StartGameLoop()
Do While IsRunning = True
Application.DoEvents()
DrawGraphics()
TickCounter()
Loop
End Sub
Private Sub TickCounter()
If tSec = TimeOfDay.Second And IsRunning = True Then
tTicks = tTicks + 1
Else
MaxTicks = tTicks
tTicks = 0
tSec = TimeOfDay.Second
End If
End Sub
End Class
答案 0 :(得分:0)
你在这里利用了很多不好的做法......
首先,使用Me.CreateGraphics()
是不好的,结果对象只能用一次绘制,这意味着你被迫多次调用它。连续调用它只会创建越来越多的图形对象,从而增加内存使用量。即使你在每次完成绘制时都要处理它们,它仍然是一个巨大的瓶颈,因为它会减慢处理速度。
其次,使用Application.DoEvents()
是 非常糟糕的行为 ,并将像这样循环地刻录CPU。除非正确使用(您不会这样做),否则会导致意外和不可预测的行为。您获得的错误是这种意外行为的一个很好的例子。
我建议您阅读此MSDN博客,它解释了为什么 不应该 使用Application.DoEvents()
:Keeping your UI Responsive and the Dangers of Application.DoEvents。
相反,为了正确执行此操作:
将Me.CreateGraphics()
替换为您的表单Paint
event,然后通过e.Graphics
对象完成所有绘图。
用Timer
替换游戏循环,不断调用Me.Invalidate()
重绘表单。
例如:
Dim WithEvents GameTimer As New Timer() With {.Interval = 1}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GameTimer.Start()
End Sub
Private GameTimer_Tick(sender As System.Object, e As System.EventArgs) Handles GameTimer.Tick
Me.Invalidate() 'Redraw the form.
TickCounter()
End Sub
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.Clear(Color.Wheat)
For X = 0 To 19
For Y = 0 To 14
Dim r As New Rectangle(X * 32, Y * 32, 32, 32)
e.Graphics.FillRectangle(Brushes.BurlyWood, r)
e.Graphics.DrawRectangle(Pens.Black, r)
Next
Next
e.Graphics.DrawString("Ticks: " & tTicks & vbCrLf & _
"TPS: " & MaxTicks, Me.Font, Brushes.Black, 650, 0)
End Sub