我目前正在vb中制作一个太空入侵者游戏(对于一个学校项目,否则我会避开这个以制作游戏......)
我有一个名为Invader的类,其中将存储每个敌人的信息。部分原因是有一个用于显示入侵者图像的图片框,我已将其添加为该类的属性。
Public Class Invader
' PROPERTIES '
Property X As Integer 'The X coordinate'
Property Y As Integer 'The Y coordinate'
Property PicBox As PictureBox 'The picturebox that holds the invader picture'
Property Type As String 'The enemy type that the invader is'
Property Health As Integer 'The health of the invader'
Property Speed As Integer 'The number of pixels that the invader moves when Move() is called'
Property Alive As Boolean 'Whether the invader is alive or not'
Property Direction As Integer 'The direction that the invader is 'facing'. 1 will be right and 0 will be left'
然后将数组声明为
Public Enemy(0 To 3, 0 To 10) As Invader
该类的实例是从CreateEnemies()
创建的Public Sub CreateEnemies()
For r = 0 To 3
For c = 0 To 10
Enemy(r, c) = New Invader
Next
Next
End Sub
现在,我有一个名为PositionEnemies()的子目录,它将每个图片框放在游戏表格上(称为frmGame),然后将图片加载到其中并显示它。
Public Sub PositionEnemies()
For r = 0 To 3
For c = 0 To 10
Enemy(r, c).PicBox = New PictureBox
Enemy(r, c).X = 0 + ((c + 1) * 110)
Enemy(r, c).Y = 362 - (((r + 1) * 100) - 100)
Enemy(r, c).Alive = True
Enemy(r, c).UpdateLocation(r, c)
Enemy(r, c).PicBox.ImageLocation = normalinvader
Enemy(r, c).PicBox.Load()
Enemy(r, c).PicBox.Height = 50
Enemy(r, c).PicBox.Width = 50
Enemy(r, c).PicBox.Visible = True
MsgBox(Enemy(r, c).PicBox.Created.ToString())
Next
Next
End Sub
我已经在最后添加了消息框,告诉我是否已经创建了图片框,目前这些图片框总是回复为" false"。
当frmGame加载时,所有这些都被调用
Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
CreateEnemies()
PositionEnemies()
End Sub
如果你已经设法完成所有这些,我遇到的问题是frmGame的加载成功完成,但实际上没有任何图片框被创建(消息框返回" false")。我已经看了这段代码这么长时间而且无法弄清楚为什么 - 我希望你们中的一个可爱的人能帮助我。
由于
答案 0 :(得分:0)
每个表单都希望您将要显示的控件添加到其Controls集合中 如果你不这样做,就不要显示......
Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateEnemies()
PositionEnemies(Me)
Me.Show()
End Sub
Public Sub PositionEnemies(ByVal f as Form)
For r = 0 To 3
For c = 0 To 10
Enemy(r, c).PicBox = New PictureBox
Enemy(r, c).X = 0 + ((c + 1) * 110)
Enemy(r, c).Y = 362 - (((r + 1) * 100) - 100)
Enemy(r, c).Alive = True
Enemy(r, c).UpdateLocation(r, c)
Enemy(r, c).PicBox.ImageLocation = normalinvader
Enemy(r, c).PicBox.Load()
Enemy(r, c).PicBox.Height = 50
Enemy(r, c).PicBox.Width = 50
Enemy(r, c).PicBox.Visible = True
'MsgBox(Enemy(r, c).PicBox.Created.ToString())
f.Controls.Add(Enemy(r, c).PicBox)
Next
Next
End Sub
此外,在您创建敌人并定位PictureBoxes后,我会将调用移至Show。当然,如果PositionEnemies位于frmGame类中,那么您可以直接使用Me引用而不传递它。
答案 1 :(得分:0)
我认为你应该添加
Me.Controls.Add(Enemy(r, c).PicBox)
在
Public Sub PositionEnemies()
以下
Enemy(r, c).PicBox.Visible = True