Collision Code

MrMk2gtx

Active member
Joined
Apr 1, 2011
Messages
29
Programming Experience
Beginner
HTML:
If (Shot.Top + Shot.Height >= Enemy1.Top) And (Shot.Top <= Enemy1.Top + Enemy1.Height) And (Shot.Left + Shot.Width >= Enemy1.Left) And (Shot.Left <= Enemy1.Left + Enemy1.Width) And Shot.Visible = True Then
            Enemy1.Visible = True
            LBLSCORE.Text = Val(LBLSCORE.Text) + 20
            Shot.Visible = False
            Shot.Left = 5000
            Enemy1.Left = -100
            Enemy1.Visible = True

       End If
If (Missile.Top + Missile.Height >= Enemy1.Top) And (Missile.Top <= Enemy1.Top + Enemy1.Height) And (Missile.Left + Missile.Width >= Enemy1.Left) And (Missile.Left <= Enemy1.Left + Enemy1.Width) And Enemy1.Visible = True Then
            Enemy1.Visible = False
            LBLSCORE.Text = Val(LBLSCORE.Text) + 25
            Missile.Visible = False
            Enemy1.Left = -100
            Enemy1.Visible = True
        End If



This is the code I have for the game, I am making but there's a problem. The enemy disappears without being shot.
There are 9 enemies moving from left to right. When one is shot, about 3 other disappear with it and on the second loop they keep disappearing.

Help!
 
Last edited:
You can hit-test simpler like this:
If Shot.Bounds.IntersectsWith(Enemy1.Bounds) Then
 
You can hit-test simpler like this:
If Shot.Bounds.IntersectsWith(Enemy1.Bounds) Then

I am sorry but it's still doing the same thing:

HTML:
'Collision Code for enemy1        
If Shot.Bounds.IntersectsWith(Enemy1.Bounds) Then            
Enemy1.Visible = True            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Left = 5000            
Enemy1.Left = -100            
Enemy1.Visible = True        
End If        

If Missile.Bounds.IntersectsWith(Enemy1.Bounds) Then            
Enemy1.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy1.Left = -100            
Enemy1.Visible = True       
End If

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'this code tells the timer to move the enemy to the right'
If Timer1.Enabled = True Then
Enemy1.Left = Enemy1.Left + 5
End If
If Enemy1.Left >= Me.ClientRectangle.Width Then
Enemy1.Left = -50
End If
End Sub
 
You say that there are 9 enemies and multiple disappear when one is shot, yet your code includes nothing but Enemy1. There's nothing in that code that is going to make anything happen to anything other than Enemy1.

By the way, the first code snippet sets Enemy1.Visible to True twice, which I'm guessing is not what you want. Logically, you should only be setting the Visible property if you have an expression that needs to be evaluated. In cases where you would assign a literal value, i.e. True or False, call Show or Hide instead.
 
You say that there are 9 enemies and multiple disappear when one is shot, yet your code includes nothing but Enemy1. There's nothing in that code that is going to make anything happen to anything other than Enemy1.

By the way, the first code snippet sets Enemy1.Visible to True twice, which I'm guessing is not what you want. Logically, you should only be setting the Visible property if you have an expression that needs to be evaluated. In cases where you would assign a literal value, i.e. True or False, call Show or Hide instead.

Thank you for that and here's all the code for the enemies and the timers that control them,

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick      
'this code tells the timer to move the enemy to the right'        
If Timer1.Enabled = True Then            
Enemy1.Left = Enemy1.Left + 5        
End If        
If Enemy1.Left >= Me.ClientRectangle.Width Then            
Enemy1.Left = -50        
End If    End Sub    

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer2.Enabled = True Then            
Enemy2.Left = Enemy2.Left + 4        
End If        
If Enemy2.Left >= Me.ClientRectangle.Width Then            
Enemy2.Left = -50        
End If    
End Sub  
  
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer3.Enabled = True Then            
Enemy3.Left = Enemy2.Left + 3        
End If        
If Enemy3.Left >= Me.ClientRectangle.Width Then            
Enemy3.Left = -50        
End If    End Sub    

Private Sub Timer4_Tick(sender As System.Object, e As System.EventArgs) Handles Timer4.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer4.Enabled = True Then            
Enemy4.Left = Enemy4.Left + 4        
End If        
If Enemy4.Left >= Me.ClientRectangle.Width Then            
Enemy4.Left = -50        
End If    
End Sub    

Private Sub Timer5_Tick(sender As System.Object, e As System.EventArgs) Handles Timer5.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer5.Enabled = True Then            
Enemy5.Left = Enemy5.Left + 5        
End If        
If Enemy5.Left >= Me.ClientRectangle.Width Then            
Enemy5.Left = -50        
End If    
End Sub    

Private Sub Timer6_Tick(sender As System.Object, e As System.EventArgs) Handles Timer6.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer6.Enabled = True Then            
Enemy6.Left = Enemy6.Left + 6        
End If        
If Enemy6.Left >= Me.ClientRectangle.Width Then            
Enemy6.Left = -50        
End If   
End Sub    

Private Sub Timer7_Tick(sender As System.Object, e As System.EventArgs) Handles Timer7.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer7.Enabled = True Then            
Enemy7.Left = Enemy7.Left + 5       
End If        
If Enemy7.Left >= Me.ClientRectangle.Width Then            
Enemy7.Left = -50        
End If    
End Sub    

Private Sub Timer8_Tick(sender As System.Object, e As System.EventArgs) Handles Timer8.Tick        
'this code tells the timer to move the enemy to the right'        
If Timer8.Enabled = True Then            
Enemy8.Left = Enemy8.Left + 4        
End If        
If Enemy8.Left >= Me.ClientRectangle.Width Then            
Enemy8.Left = -50        
End If    
End Sub    

Private Sub CheckHit()        
'Collision Code for enemy1        
If (Shot.Top + Shot.Height >= Enemy1.Top) And (Shot.Top <= Enemy1.Top + Enemy1.Height) And (Shot.Left + Shot.Width >= Enemy1.Left) And (Shot.Left <= Enemy1.Left + Enemy1.Width) And Shot.Visible = True Then            
Enemy1.Visible = True            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Left = 5000            
Enemy1.Left = -100            
Enemy1.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy1.Top) And (Missile.Top <= Enemy1.Top + Enemy1.Height) And (Missile.Left + Missile.Width >= Enemy1.Left) And (Missile.Left <= Enemy1.Left + Enemy1.Width) And Enemy1.Visible = True Then            
Enemy1.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy1.Left = -100            
Enemy1.Visible = True        
End If        

'Collision Code for enemy2        
If (Shot.Top + Shot.Height >= Enemy2.Top) And (Shot.Top <= Enemy2.Top + Enemy2.Height) And (Shot.Left + Shot.Width >= Enemy2.Left) And (Shot.Left <= Enemy2.Left + Enemy1.Width) And Shot.Visible = True Then            
Enemy2.Visible = True            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Left = 5000            
Enemy2.Left = -100            
Enemy2.Visible = True
End If        
If (Missile.Top + Missile.Height >= Enemy2.Top) And (Missile.Top <= Enemy2.Top + Enemy2.Height) And (Missile.Left + Missile.Width >= Enemy2.Left) And (Missile.Left <= Enemy2.Left + Enemy2.Width) And Enemy2.Visible = True Then            
Enemy2.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25           
Missile.Visible = False            
Enemy2.Left = -100            
Enemy2.Visible = True       
End If        

'Collision Code for enemy3        
If Shot.Top < Enemy3.Top + Enemy3.Height And Shot.Left > Enemy3.Left And Shot.Left < Enemy3.Left + Enemy3.Width Then            
Enemy3.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Enemy3.Left = -100            
Enemy3.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy3.Top) And (Missile.Top <= Enemy3.Top + Enemy3.Height) And (Missile.Left + Missile.Width >= Enemy3.Left) And (Missile.Left <= Enemy3.Left + Enemy3.Width) And Enemy3.Visible = True Then            
Enemy3.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy3.Left = -100            
Enemy3.Visible = True        
End If        

'Collision Code for enemy4        
If (Shot.Top + Shot.Height >= Enemy4.Top) And (Shot.Top <= Enemy4.Top + Enemy4.Height) And (Shot.Left + Shot.Width >= Enemy4.Left) And (Shot.Left <= Enemy4.Left + Enemy4.Width) And Enemy4.Visible = True Then            
Enemy4.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Top = 441            
Shot.Left = 5000            
Enemy4.Left = -100            
Enemy4.Visible = True        
End If       
If (Missile.Top + Missile.Height >= Enemy4.Top) And (Missile.Top <= Enemy4.Top + Enemy4.Height) And (Missile.Left + Missile.Width >= Enemy4.Left) And (Missile.Left <= Enemy4.Left + Enemy4.Width) And Enemy4.Visible = True Then            
Enemy4.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25           
Missile.Visible = False            
Enemy4.Left = -100            
Enemy4.Visible = True        
End If        

'Collision Code for enemy5        
If (Shot.Top + Shot.Height >= Enemy5.Top) And (Shot.Top <= Enemy5.Top + Enemy5.Height) And (Shot.Left + Shot.Width >= Enemy5.Left) And (Shot.Left <= Enemy5.Left + Enemy5.Width) And Enemy5.Visible = True Then            
Enemy5.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Top = 441            
Shot.Left = 5000            
Enemy5.Left = -100            
Enemy5.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy5.Top) And (Missile.Top <= Enemy5.Top + Enemy5.Height) And (Missile.Left + Missile.Width >= Enemy5.Left) And (Missile.Left <= Enemy5.Left + Enemy5.Width) And Enemy5.Visible = True Then            
Enemy5.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy5.Left = -100            
Enemy5.Visible = True        
End If       

'Collision Code for enemy6        
If (Shot.Top + Shot.Height >= Enemy6.Top) And (Shot.Top <= Enemy6.Top + Enemy6.Height) And (Shot.Left + Shot.Width >= Enemy6.Left) And (Shot.Left <= Enemy6.Left + Enemy6.Width) And Enemy6.Visible = True Then            
Enemy6.Visible = False           
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Top = 441           
Shot.Left = 5000            
Enemy6.Left = -100            
Enemy6.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy6.Top) And (Missile.Top <= Enemy6.Top + Enemy6.Height) And (Missile.Left + Missile.Width >= Enemy6.Left) And (Missile.Left <= Enemy6.Left + Enemy6.Width) And Enemy6.Visible = True Then            
Enemy6.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy6.Left = -100            
Enemy6.Visible = True        
End If        

'Collision Code for enemy7        
If (Shot.Top + Shot.Height >= Enemy7.Top) And (Shot.Top <= Enemy7.Top + Enemy7.Height) And (Shot.Left + Shot.Width >= Enemy7.Left) And (Shot.Left <= Enemy7.Left + Enemy7.Width) And Enemy7.Visible = True Then            
Enemy7.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Top = 441            
Shot.Left = 5000            
Enemy7.Left = -100            
Enemy7.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy7.Top) And (Missile.Top <= Enemy7.Top + Enemy7.Height) And (Missile.Left + Missile.Width >= Enemy7.Left) And (Missile.Left <= Enemy7.Left + Enemy7.Width) And Enemy7.Visible = True Then            
Enemy7.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy7.Left = -100            
Enemy7.Visible = True        
End If        

'Collision Code for enemy8        
If (Shot.Top + Shot.Height >= Enemy8.Top) And (Shot.Top <= Enemy8.Top + Enemy8.Height) And (Shot.Left + Shot.Width >= Enemy8.Left) And (Shot.Left <= Enemy8.Left + Enemy8.Width) And Enemy8.Visible = True Then            
Enemy8.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 20            
Shot.Visible = False            
Shot.Top = 441            
Shot.Left = 5000            
Enemy8.Left = -100            
Enemy8.Visible = True        
End If        
If (Missile.Top + Missile.Height >= Enemy8.Top) And (Missile.Top <= Enemy8.Top + Enemy8.Height) And (Missile.Left + Missile.Width >= Enemy8.Left) And (Missile.Left <= Enemy8.Left + Enemy8.Width) And Enemy8.Visible = True Then            
Enemy8.Visible = False            
LBLSCORE.Text = Val(LBLSCORE.Text) + 25            
Missile.Visible = False            
Enemy8.Left = -100            
Enemy8.Visible = True        
End If

'this code stops the timers at the reach of 500'        
If LBLSCORE.Text = 5000000000 Then            
TimerMain.Stop()           
Timer1.Stop()            
Timer2.Stop()            
Timer3.Stop()            
Timer4.Stop()            
Timer5.Stop()            
Timer6.Stop()            
Timer7.Stop()            
Timer8.Stop()            
Me.Hide()            
MsgBox("MISSION ACCOMPLISHED")        
End If    
End Sub
 
Last edited:
You might try posting the code with indenting so it can be comfortably read. If the code is not HTML code then why use HTML code tags? Use [xcode=vb]your code here[/xcode]
 
You might try posting the code with indenting so it can be comfortably read. If the code is not HTML code then why use HTML code tags? Use
your code here

Hi,

Is there a way to have just one picturebox representing the enemy and spawning multiple times instead of having mutiple pictureboxes and timers.
Any help would be appreciated.

Thanks in advance
 
Back
Top