我的程序是WIP RPG对战游戏,用户必须与怪物战斗才能进步。我希望每当玩家使用他们的近战攻击时都这样做,攻击量是随机的。但是每次加载程序时,玩家第一次按下按钮触发攻击时,即使在点击按钮时发生其他事情,也不会发生攻击。比如其他按钮被禁用。
这是按钮的存根。如有必要,我可以发布完整的代码。
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
tmrRoundCount.Start()
tmrTurn.Start()
btnMeleeAttack.Enabled = False
btnRangedAttack.Enabled = False
btnDefend.Enabled = False
btnUseItem.Enabled = False
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
lblEnemyStats.Text = dblEnemyHealth & "/" & dblEnemyHealthMax & " HP" & vbCrLf & dblEnemyDefense & " DEF" & vbCrLf & dblEnemyAttack & " ATK"
Randomize()
dblRandomAttackVar = CInt(Int((9 - 1 + 1) * Rnd() + 1))
If dblRandomAttackVar = 1 Then
dblMelee = 3
End If
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
If dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
End If
If dblRandomAttackVar = 4 And intMeleeAttackCount <> 0 Then
dblMelee = 3.75
End If
If dblRandomAttackVar = 5 And intMeleeAttackCount <> 0 Then
dblMelee = 4
End If
If dblRandomAttackVar = 6 And intMeleeAttackCount <> 0 Then
dblMelee = 4.25
End If
If dblRandomAttackVar = 7 And intMeleeAttackCount <> 0 Then
dblMelee = 4.5
End If
If dblRandomAttackVar = 8 And intMeleeAttackCount <> 0 Then
dblMelee = 4.75
End If
If dblRandomAttackVar = 9 And intMeleeAttackCount <> 0 Then
dblMelee = 5
End If
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
End If
End Sub
答案 0 :(得分:0)
如果没有完整的代码,这可能是一个猜测,但我认为你的条件是这个问题的原因:
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
我认为,对于第一次攻击,intMeleeAttackCount
实际上是0.这意味着,除非第一次攻击有第1次攻击,否则攻击将永远不会被执行,因为intMeleeAttackCount
没有&#39}不符合条件。
无关:你可以像这样预备你的代码:
If dblRandomAttackVar = 1 Then
dblMelee = 3
ElseIf dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
ElseIf dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
'...
End If
答案 1 :(得分:0)
我正在摆弄你的代码,以帮助你的方式,但让我们先在这里试试这个:
<强>答案强>
你说&#34;没有发生攻击&#34;。您在lblEnemyStats中获得状态消息的唯一反馈是什么?
然后你应该在计算之后而不是之前更新。
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
tmrRoundCount.Start()
tmrTurn.Start()
btnMeleeAttack.Enabled = False
btnRangedAttack.Enabled = False
btnDefend.Enabled = False
btnUseItem.Enabled = False
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
Randomize()
dblRandomAttackVar = CInt(Int((9 - 1 + 1) * Rnd() + 1))
If dblRandomAttackVar = 1 Then
dblMelee = 3
End If
If dblRandomAttackVar = 2 And intMeleeAttackCount <> 0 Then
dblMelee = 3.25
End If
If dblRandomAttackVar = 3 And intMeleeAttackCount <> 0 Then
dblMelee = 3.5
End If
If dblRandomAttackVar = 4 And intMeleeAttackCount <> 0 Then
dblMelee = 3.75
End If
If dblRandomAttackVar = 5 And intMeleeAttackCount <> 0 Then
dblMelee = 4
End If
If dblRandomAttackVar = 6 And intMeleeAttackCount <> 0 Then
dblMelee = 4.25
End If
If dblRandomAttackVar = 7 And intMeleeAttackCount <> 0 Then
dblMelee = 4.5
End If
If dblRandomAttackVar = 8 And intMeleeAttackCount <> 0 Then
dblMelee = 4.75
End If
If dblRandomAttackVar = 9 And intMeleeAttackCount <> 0 Then
dblMelee = 5
End If
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
End If
lblEnemyStats.Text = dblEnemyHealth & "/" & dblEnemyHealthMax & " HP" & vbCrLf & dblEnemyDefense & " DEF" & vbCrLf & dblEnemyAttack & " ATK"
End Sub
澄清
我认为您的代码会执行它应该执行的操作,但是您的UI更新得太晚了。让我们说每个按钮都是一个&#34; Tick&#34;在游戏的时间轴上。那就是
0 - &gt;点击 - &gt; 1 - &gt;点击 - &gt; 2 - &gt;点击 - &gt; 3等等。 这就是你的#34;当前状态#34;。但是用户界面落后了,所以你对所显示的内容的解释也是错误的,因为你看到的不是你所拥有的 - 你的问题证明 - 你认为按钮没有做任何事情然后开始工作,是神秘的&#34;并将导致雪崩的错误。
游戏:0-&gt; 1-&gt; 2-&gt; 3-&gt; 4-&gt;怪物死了?!? UI:0-&gt; 0-&gt; 1-&gt; 2-&gt; 3-&gt;怪物还没死???
<强>加成强>
这可能会使调试更容易。使用评论,他们是你学习编程状态的最好朋友。 但我并不是要求理解超过一半的代码。
Private Sub toggleFightButtons(onOff As Boolean)
btnMeleeAttack.Enabled = onOff
btnRangedAttack.Enabled = onOff
btnDefend.Enabled = onOff
btnUseItem.Enabled = onOff
End Sub
Private Sub showStats()
Dim stats As String = String.Format("{0}/{1} HP {2} DEF {3} ATK", dblEnemyHealth, dblEnemyHealthMax, dblEnemyDefense, dblEnemyAttack)
lblEnemyStats.Text = stats
End Sub
Public Sub btnMeleeAttack_Click(sender As Object, e As EventArgs) Handles btnMeleeAttack.Click
'Suspend interaction with the game
tmrRoundCount.Start()
tmrTurn.Start()
'????
If intRoundCountTicks = 0 Then
dblEnemyHealthOld = dblEnemyHealthMax
End If
'Introduce a little bit of RNG
dblEnemyHealth = dblEnemyHealth - (dblMelee / dblEnemyDefense)
Randomize()
dblRandomAttackVar = CInt(Int(9 * Rnd() + 1))
'Calculate the result
If intMeleeAttackCount = 0 Then
dblMelee = 4.5
Else
dblMelee = 3 + (0.25 * (dblRandomAttackVar - 1))
End If
'Update UI to show what it is we did
showStats()
End Sub
这与您的代码相同,可以更简化。