我有一个VB.NET类库,ConsoleControls,它有一些类对象,用于在控制台模式下模拟Forms对象。例如,有一个ProgressBar类,它绘制一个设置为某个值的ASCII进度条。我一直在我的主项目TheGame中使用这个库。
代码是有用的,但是当我在主项目中使用类对象和方法时,我在调试时遇到了奇怪的行为。例如,当我调用ProgressBar类的Draw方法时,显示该行即将执行的黄色箭头出现在该方法的End Sub中。然后,它开始运行一个完全不同的类TextBox,它与ProgressBar无关。它多次重复某些行。就好像调试器运行完全不同的代码部分而不是它显示正在运行的代码。
这是ProgressBar类:
Public Class ProgressBar
Public InactiveColour As ConsoleColor
Public ActiveColour As ConsoleColor
Public Position As Point
Public Length As Integer
Public Value As Integer
Public MaxValue As Integer
Public Sub New(_Position As Point, _Length As Integer, _InactiveColour As ConsoleColor, _ActiveColour As ConsoleColor)
Position = _Position
Length = _Length
InactiveColour = _InactiveColour
ActiveColour = _ActiveColour
End Sub
Public Sub SetValue(_Value As Integer, _MaxValue As Integer)
Value = _Value
MaxValue = _MaxValue
End Sub
Public Sub IncrementValue()
If Value + 1 <= MaxValue Then Value += 1
End Sub
Public Sub DecrementValue()
If Value - 1 >= 0 Then Value -= 1
End Sub
Public Sub Draw()
Dim ActiveBars, InactiveBars As Integer
Dim Divisor As Double
Divisor = MaxValue / Length
ActiveBars = CInt(Value / Divisor)
InactiveBars = Length - ActiveBars
Console.SetCursorPosition(Position.X, Position.Y)
Console.ForegroundColor = ActiveColour
For i = 1 To ActiveBars
Console.Write(Block)
Next
Console.ForegroundColor = InactiveColour
For i = 1 To InactiveBars
Console.Write(Block)
Next
Console.ResetColor()
End Sub
End Class
这是在我的主项目中做奇怪事情的代码:
Dim PlayerHealthBar As ProgressBar
PlayerHealthBar = New ProgressBar(New Point(30, 4), 32, ConsoleColor.DarkGray, ConsoleColor.White)
PlayerHealthBar.SetValue(1, 10)
PlayerHealthBar.Draw()
有关可能发生的事情的任何建议吗?