(VB.net)将一个图片框作为一个类的属性

时间:2023-01-15 16:51:56

I am currently making a space invaders game in vb (for a school project otherwise I would steer clear of this for making a game...)

我目前正在vb中制作一个太空入侵者游戏(对于一个学校项目,否则我会避开这个以制作游戏......)

I have a class called Invader, where the information on each enemy will be stored. Part of this is having a picturebox to display the image of the invader, which I have added as a property of the class.

我有一个名为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'

The array is then declared as

然后将该数组声明为

    Public Enemy(0 To 3, 0 To 10) As Invader

The instances of the class are created from CreateEnemies()

该类的实例是从CreateEnemies()创建的

Public Sub CreateEnemies()
    For r = 0 To 3
        For c = 0 To 10
            Enemy(r, c) = New Invader
        Next
    Next
End Sub

Now, I have a sub called PositionEnemies() which will position each picturebox on the game form (called frmGame) and then load the picture into it and display it.

现在,我有一个名为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

I have added the messagebox at the end to tell me if the picturebox has been created, which at the moment always comes back as "false".

我已经在最后添加了消息框,告诉我是否已经创建了图片框,目前这个图片框总是回复为“假”。

All of this is then called when frmGame loads

然后在frmGame加载时调用所有这些

Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Show()
    CreateEnemies()
    PositionEnemies()
End Sub

If you have managed to follow all that, the problem I have is that the load of frmGame completes successfully, but none of the pictureboxes are actually created for some reason (the messagebox returns "false"). I have looked at this code for so long and not been able to figure out why - I hope one of you lovely people could help me out here.

如果你已经设法完成所有这些,我遇到的问题是frmGame的加载成功完成,但实际上没有任何图片框是由于某种原因创建的(消息框返回“false”)。我已经看了这段代码这么长时间而且无法弄清楚为什么 - 我希望你们中的一个可爱的人能帮助我。

Thanks

谢谢

2 个解决方案

#1


0  

Every form wants that you add the controls to be displayed to its Controls collection.
If you don't do it, no display....

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

Also, I would move the call to Show after you have created your Enemies and positioned the PictureBoxes. Of course, if the PositionEnemies is inside the frmGame class then you could use directly the Me reference without passing it.

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

#2


0  

I think you should add

我想你应该补充一下

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

in

Public Sub PositionEnemies()

below

下面

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

#1


0  

Every form wants that you add the controls to be displayed to its Controls collection.
If you don't do it, no display....

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

Also, I would move the call to Show after you have created your Enemies and positioned the PictureBoxes. Of course, if the PositionEnemies is inside the frmGame class then you could use directly the Me reference without passing it.

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

#2


0  

I think you should add

我想你应该补充一下

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

in

Public Sub PositionEnemies()

below

下面

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