Hello, I'm just beginning to learn VB.net and need some help with my programme. It's a game that I've been making through following tutorials on YouTube and there is an error which I don't know how to fix. Essentially in this 2D game you are a ship (picture box) at the bottom of the screen and you have these aliens (picture boxes) appearing at the top of the screen and working their way down from left to right and then moving down once they hit a border.
I had this working with just one picture box however now I've tried to add a dynamic array in which a pre-set number of aliens will appear. There are no errors in the error list however when I click on run (the play button) it comes up with "Object reference not set to an instance of an object." which after some research turns out to be because where it says Aliens(x).Left += AlienSpeed it cannot do the .Left bit because there is no value for Alien(x) which I don't know how to fix, the whole sub is below for MoveAlien if that is helpful, I've also put a ">" at the start of each line where the error appears. Many thanks, Bulldog94
Private Sub MoveAlien()
For Me.x = 1 To NumOfAliens
If AlienRight(x) = True Then
> Aliens(x).Left += AlienSpeed
Else
> Aliens(x).Left -= AlienSpeed
End If
> If Aliens(x).Left + Aliens(x).Width > Me.ClientRectangle.Width Then
AlienRight(x) = False
> Aliens(x).Top += AlienDrop
End If
> If Aliens(x).Left < Me.ClientRectangle.Left Then
AlienRight(x) = True
> Aliens(x).Top += AlienDrop
End If
Next
End Sub
I had this working with just one picture box however now I've tried to add a dynamic array in which a pre-set number of aliens will appear. There are no errors in the error list however when I click on run (the play button) it comes up with "Object reference not set to an instance of an object." which after some research turns out to be because where it says Aliens(x).Left += AlienSpeed it cannot do the .Left bit because there is no value for Alien(x) which I don't know how to fix, the whole sub is below for MoveAlien if that is helpful, I've also put a ">" at the start of each line where the error appears. Many thanks, Bulldog94
Private Sub MoveAlien()
For Me.x = 1 To NumOfAliens
If AlienRight(x) = True Then
> Aliens(x).Left += AlienSpeed
Else
> Aliens(x).Left -= AlienSpeed
End If
> If Aliens(x).Left + Aliens(x).Width > Me.ClientRectangle.Width Then
AlienRight(x) = False
> Aliens(x).Top += AlienDrop
End If
> If Aliens(x).Left < Me.ClientRectangle.Left Then
AlienRight(x) = True
> Aliens(x).Top += AlienDrop
End If
Next
End Sub
Last edited: