如何将新PictureBox添加到表单和数组

时间:2015-08-28 04:03:20

标签: vb.net

G'每个人,我目前正在开展一个评估的重大项目,并且遇到了错误。我几乎没有任何数组经验。基本上,在一个阵列中有一大堆随机图片框,它们随机出现在屏幕左侧的随机位置并向右移动。您可以控制GIF图像直升机向上,向下,向前(向左)和向后(向右)移动。当按下空间时,它会创建一个新的图片框,其中包含导弹发射和旋转的动画图像,该图像移动到屏幕的左侧。当它到达终点时它会自行删除,但如果它击中一个对象,它会通过计时器和图片框内的图像列表发生爆炸,然后删除自己并隐藏命中图像。

我附上了与导弹阵列有关的所有代码。

'一般声明

Public Class frmPlay
Public Vertical As Integer
Public Horizontal As Integer
Dim WhichKey As String
Dim NumofObjs As Integer
Public F As Integer
Dim Missile() As PictureBox
Public ABC As Integer

'要消防

Private Sub frmPlay_KeyDown
Select Case e.KeyValue
    Case Keys.Space
            If GameMode = "Protector" Then
                Dim Missile As PictureBox = New PictureBox
                Missile.Height = "14"
                Missile.Width = "72"
                Missile.ImageLocation = "graphics-helicopters-381058.gif"
                Missile.Enabled = True
                Missile.Show()
                Missile.Tag = "Missile"
                Me.Controls.Add(Missile)
                CentreOn(picChar, Missile)
                ABC += 1
                ReDim Missile(ABC)
            End If

'对象移动事件中的碰撞事件

Dim ABC As Integer
    For Each obj In Me.Controls
        If GameMode = "Protector" Then
            If TypeOf obj Is PictureBox AndAlso obj.Tag.Contains("Obj") Then
                obj.Left += 20
                For ABC = 0 To 2 Step 1
                    If Collision(obj, Missile(ABC)) Then
                        If obj.Tag.Contains(lblCriteria.Text) Then
                            Missile(ABC).Visible = False
                            Missile(ABC).Enabled = False
                            obj.Visible = False
                            If picExplosion1.Visible = True And picExplosion2.Visible = False And picExplosion3.Visible = False Then
                                picExplosion2.Visible = True
                                tmrExplosion2.Start()
                                CentreOn(obj, picExplosion2)
                            ElseIf picExplosion1.Visible = True And picExplosion2.Visible = True And picExplosion3.Visible = False Then
                                picExplosion3.Visible = True
                                tmrExplosion3.Start()
                                CentreOn(obj, picExplosion3)
                            ElseIf picExplosion1.Visible = False Then
                                picExplosion1.Visible = True
                                tmrExplosion1.Start()
                                CentreOn(obj, picExplosion1)
                            Else
                            End If
                        End If
                    End If
                Next ABC
            End If
        End If

'移动导弹

Private Sub tmrMissiles_Tick(sender As System.Object, e As System.EventArgs) Handles tmrMissiles.Tick
    For CBA = 0 To 2 Step 1
        Missile(CBA).Left -= 10
        If Missile(CBA).Left <= -Missile(CBA).Width Then
            Me.Controls.Remove(Missile(CBA))
        End If
    Next
End Sub

&#39;在formLoad事件中声明数组

Private Sub frmPlay_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim ABC As Integer = 0
    For Each obj In Me.Controls
        If TypeOf obj Is PictureBox AndAlso obj.Tag = "Missile" Then
            Missile(ABC) = obj
            obj.Hide()
            obj.Enabled = False
        End If
    Next

还有一大堆与图形相关的其他代码以及敌人的移动和角色移动等等但它们都完美无缺。而且我确实让它在导弹射击事件中工作得很好,但是你一次只能发射三枚,并且不得不等待其中一枚击中敌人或者到达屏幕的末端。

那么我该如何制造它以便我可以为导弹创建一个数组,它开始为空,然后当按下空间时它创建一个新的picBox,添加到控件和数组并使用导弹阵列计时器tick事件移动?请保持代码与我的风格相同,所以我不需要学习一大堆新东西,谢谢。

谢谢大家,Burge-o

0 个答案:

没有答案