I have been set a project from school to design snake in VB.Net but i am struggling to dynamically add the user control which acts as one of the dots in the body of the snake. Every time i add a dot on to the snake the previous vanishes. I assume this is due to me overwriting the previous one by creating a new instance of the object but i cannot find a way of adding a new one in.
Dim body As New Dot
Sub AddToSnake(ByVal i As Integer)
'add 1 dot on to the back of the snake
body.Location = New Point(50 + i, 50)
body.Visible = True
Me.Controls.Add(body)
End Sub
I have tried using body(i) and other ways of adding a new instance of the object but am stuck.
The subroutine will be called up when a new control is needed.
You aren’t actually creating a new instance anywhere, your code only ever creates a single instance of
Dotand modifies the single instance’s location inAddToSnake. In order to overcome the problem, create a new instance ofDotinsideAddToSnake.That said, user controls aren’t the correct way for this anyway. You should paint your snake body on a
PictureBoxcontrol, and maintain the body as a aList(Of Point). Then you can simply add new points at the end of the list.