(VB.net) 将图片框作为 class 的 属性

(VB.net) Having a picturebox as a property of a class

我目前正在 vb 制作一个 space 入侵者游戏(对于学校项目,否则我会为了制作游戏而避开这个...)

我有一个名为 Invader 的 class,每个敌人的信息都将存储在其中。其中一部分是有一个图片框来显示入侵者的图像,我将其添加为 class.

的 属性
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

class 的实例是从 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 的加载成功完成,但是 none 的图片框实际上是出于某种原因创建的(消息框 returns "false")。我已经查看这段代码很长时间了,但一直无法弄清楚为什么 - 我希望你们中的一位可爱的人可以帮助我解决这个问题。

谢谢

每个表单都希望您将要显示的控件添加到其 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

此外,我会在您创建敌人并定位 PictureBox 后将调用移至 Show。当然,如果 PositionEnemies 在 frmGame 中 class 那么你可以直接使用 Me 引用而不传递它。

我认为你应该添加

Me.Controls.Add(Enemy(r, c).PicBox)

Public Sub PositionEnemies()

低于

Enemy(r, c).PicBox.Visible = True