对不起,很长的一段代码。在我正在设计的游戏中,我的代码从 checkbox.checked if 语句开始,对于每个单击的复选框,当前重复 5 次——每个复选框等于游戏中玩家手中的一张牌。
我想知道的是我能做些什么来让它变得更有活力。即 CardCheckBox1.Checked 对象/方法。如上所述,代码重复了 5 次,因为我有 5 个现有复选框。有没有办法将复选框放入数组或集合中,所以当我单击复选框并点击播放按钮时,它相当于点击 CardCheckBox2.checked = true、CardCheckBox2.checked = true 等。一旦我弄清楚如何为了使该部分动态化,我终于可以开始使其余代码动态化,因为我假设制作动态标签代码的过程与复选框等非常相似。
我有过
Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}
在模块化级别、Form_Load 过程甚至 Play_Button 过程中发布,但我总是在 CardCheckBox(n).Checked 部分出现空引用异常
If CardCheckBoxArray(0).Checked = True And Player1HandGroup(Number1).QuantityInteger > 0 Then
所以我不知道该去哪里。长一段代码是我的整个 PlayButton_Click 过程,除了其他 CardCheckBox if 语句。
Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
Dim n As Integer
Dim WeaponDiscardInteger As Integer = 1
AtkPlayerDialog.ShowDialog()
'Code for choosing which player to attack
If AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Cancel Then
n = 2
ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.OK Then
n = 1
ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Abort Then
n = 3
ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Retry Then
n = 4
ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Ignore Then
n = 5
End If
'playing card 1
If CardCheckBox1.Checked = True And Player1HandGroup(Number1).QuantityInteger > 0 Then
'Subtract Hitpoints when damage is delt
Player1HandGroup(n).HitPoints -= Player1HandGroup(Number1).DamageInteger
HitPoints1.Text = Player1HandGroup(1).HitPoints.ToString
HitPoints2.Text = Player1HandGroup(2).HitPoints.ToString
HitPoints3.Text = Player1HandGroup(3).HitPoints.ToString
HitPoints4.Text = Player1HandGroup(4).HitPoints.ToString
HitPoints5.Text = Player1HandGroup(5).HitPoints.ToString
'When player plays hand, card quantity is removed from hand to discard pile.
Player1HandGroup(Number1).QuantityInteger -= 1
DiscardGroup(Number1).QuantityInteger += 1
'Shuffle Deck from Discard Pile if Deck is out of cards
Call DiscardPile()
'Reset Number Generator, unless weapon isn't discard
Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)
If DeckGroup(Number).QuantityInteger > 0 Then
'Grab New Card From Deck
DeckGroup(Number).QuantityInteger -= 1
Player1HandGroup(Number).QuantityInteger += 1
Card1Type = Player1HandGroup(Number).CardType
CardCheckBox1.Text = Player1HandGroup(Number).CardNameString
Number1 = Number
Else
Call PlayElse()
End If