07-13-2012, 01:41 PM
Well, I can definitely see your problem here! Well, the generally accepted solution to this problem would be to create an array which holds all of the bullets of the player, and every time the timer ticks, each bullet is moved. I have put what I am talking about into code:
Pastebin Version: <!-- m --><a class="postlink" href="http://pastebin.com/1t5iazRR">http://pastebin.com/1t5iazRR</a><!-- m -->
So, essentially what we are doing is keeping track of all the bullets that the player has actually shot, and removing them once they go off the screen. In order to apply this procedure, simply make the timer call PlayerBullets_Act every time the timer ticks.
Code:
Private playerBullets As List(Of PictureBox)
Public Sub Player_Shoot(Sender As Object, e As System.EventArgs)
'This method creates the new bullet!
Dim pro As New PictureBox
pro.Image = proj
pro.Visible = True
pro.Width = 9
pro.Height = 32
pro.Top = Player.Top - 20
pro.Left = Player.Left + 20
Controls.Add(pro)
playerBullets.Add(pro)
End Sub
Public Sub PlayerBullets_Act(sender As Object, e As System.EventArgs)
'This method moves the player bullets and detects their collisions. It should be called every
'time the timer ticks
For Each bullet As PictureBox In playerBullets
If (bullet.Location.Y <= 0) Then
'The bullet has moved off of the screen, we need to remove it to conserve system resources
Controls.Remove(bullet)
playerBullets.Remove(bullet)
End If
'TODO: Check for collisions with enemies here
bullet.Location = New System.Drawing.Point(bullet.Location.X, bullet.Location.Y - 2) 'Move the bullet up
Next
End Sub
Pastebin Version: <!-- m --><a class="postlink" href="http://pastebin.com/1t5iazRR">http://pastebin.com/1t5iazRR</a><!-- m -->
So, essentially what we are doing is keeping track of all the bullets that the player has actually shot, and removing them once they go off the screen. In order to apply this procedure, simply make the timer call PlayerBullets_Act every time the timer ticks.