EDN Admin
Well-known member
Hello everyone,
Whilst trying to make a simple game ive come across the need to put pictureboxes into an array.
This is what i have..
<pre class="prettyprint" style=" Dim numEnemies As Integer = 20
Dim enemies(numEnemies) As PictureBox
Dim x As Integer
Dim randX As Integer
Dim randY As Integer
Public Sub assignEnemies()
enemies(1) = PictureBox1
enemies(2) = PictureBox2
enemies(3) = PictureBox3
enemies(4) = PictureBox4
enemies(5) = PictureBox5
enemies(6) = PictureBox6
enemies(7) = PictureBox7
enemies(8) = PictureBox8
enemies(9) = PictureBox9
enemies(10) = PictureBox10
enemies(11) = PictureBox11
enemies(12) = PictureBox12
enemies(13) = PictureBox13
enemies(14) = PictureBox14
enemies(15) = PictureBox15
enemies(16) = PictureBox16
enemies(17) = PictureBox17
enemies(18) = PictureBox18
enemies(19) = PictureBox19
enemies(20) = PictureBox20
End Sub
// some things that dont involve the picturebox array are inbetween
Public Sub enemiesMove()
For x = 1 To numEnemies
getRandom()
enemies(x).Location = New Point(randX, randY)
Next
End Sub
Public Sub getRandom()
Randomize()
randX = Rnd() * 330
randY = Rnd() * 330
End Sub [/code]
<br/>
What im trying to get it to do is it will generate two random co-ordinates (the x and y) of the form and assign them to a picturebox, but for every picturebox it will generate a new set of co-ordinates.
i keep getting the "Object reference not set to an instance of an object." error on this line
<pre class="prettyprint" style=" enemies(x).Location = New Point(randX, randY)[/code]
What am i doing wrong? do i have to reference each pictureBox individually?
Im sorry if i havent explained this properly, im still kinda new with VB :/
Thank you in advance!
AussieTerra
<br/>
<br/>
<br/>
<br/>
View the full article
Whilst trying to make a simple game ive come across the need to put pictureboxes into an array.
This is what i have..
<pre class="prettyprint" style=" Dim numEnemies As Integer = 20
Dim enemies(numEnemies) As PictureBox
Dim x As Integer
Dim randX As Integer
Dim randY As Integer
Public Sub assignEnemies()
enemies(1) = PictureBox1
enemies(2) = PictureBox2
enemies(3) = PictureBox3
enemies(4) = PictureBox4
enemies(5) = PictureBox5
enemies(6) = PictureBox6
enemies(7) = PictureBox7
enemies(8) = PictureBox8
enemies(9) = PictureBox9
enemies(10) = PictureBox10
enemies(11) = PictureBox11
enemies(12) = PictureBox12
enemies(13) = PictureBox13
enemies(14) = PictureBox14
enemies(15) = PictureBox15
enemies(16) = PictureBox16
enemies(17) = PictureBox17
enemies(18) = PictureBox18
enemies(19) = PictureBox19
enemies(20) = PictureBox20
End Sub
// some things that dont involve the picturebox array are inbetween
Public Sub enemiesMove()
For x = 1 To numEnemies
getRandom()
enemies(x).Location = New Point(randX, randY)
Next
End Sub
Public Sub getRandom()
Randomize()
randX = Rnd() * 330
randY = Rnd() * 330
End Sub [/code]
<br/>
What im trying to get it to do is it will generate two random co-ordinates (the x and y) of the form and assign them to a picturebox, but for every picturebox it will generate a new set of co-ordinates.
i keep getting the "Object reference not set to an instance of an object." error on this line
<pre class="prettyprint" style=" enemies(x).Location = New Point(randX, randY)[/code]
What am i doing wrong? do i have to reference each pictureBox individually?
Im sorry if i havent explained this properly, im still kinda new with VB :/
Thank you in advance!
AussieTerra
<br/>
<br/>
<br/>
<br/>
View the full article