Public Function CollisionMovingImage(MovingImage As Variant, _
moveLeft As Integer, moveTop As Integer, _ Optional StaticImage As Variant) As Boolean On Error Goto ErrHandler: 'If one of the parameters is not found or 'some error happen in the function, it will then exit. Dim MovingLeft, MovingRight, MovingTop, MovingBottom As Integer 'The Moving variables are used to get in fos about the MovingImage. MovingLeft = MovingImage.Left + moveLeft MovingRight = (MovingImage.Left + moveLeft) + MovingImage.Width MovingTop = MovingImage.Top + moveTop MovingBottom = (MovingImage.Top + moveTop) + MovingImage.Height Dim okLeft, okTop As Boolean 'okLeft is use to see if the MovingImag e has a point of its width 'in commun with the Static Image. The okTop is used to see if it 'happens with the height. okLeft = True okTop = True 'They are set to true by default to allow the moving 'of the MovingImage if there is no StaticImage. If IsMissing(StaticImage) = False Then 'Execute the verification only if the 'StaticImage argument is used. Dim StaticLeft, StaticRight, StaticTop, StaticBottom As String 'The Static variables are used to get infos about 'the StaticImage. StaticLeft = StaticImage.Left StaticRight = StaticImage.Left + StaticImage.Width StaticTop = StaticImage.Top StaticBottom = StaticImage.Top + StaticImage.Height Dim i As Integer 'Verify if the MovingImage has a point 'of its width in commun with the StaticImage. For i = StaticLeft To StaticRight If (MovingLeft = i) Or (MovingRight = i) Then okLeft = False End If Next i 'Verify if the MovingImage has a point of 'its height in commun with the StaticImage. For i = StaticTop To StaticBottom If (MovingBottom = i) Or (MovingTop = i) Then okTop = False End If Next i 'Don't move the MovingPicture if there 'would be a collision. If okTop = False And okLeft = False Then 'Return true because the two objects 'would have a commun point. CollisionMovingImage = True Goto ErrHandler: End If End If 'Move the MovingImage... 'You could remove the two following lines if you 'wanted the function to only tell you ifthere would 'be a collision or no. MovingImage.Left = MovingLeft MovingImage.Top = MovingTop 'Return false because there have been no collision CollisionMovingImage = False ErrHandler: End Function Inputs:MovingImage = the image to move moveLeft= the Left movement of the MovingImage moveTop = the Top movement of the MovingImage StaticImage = the image you don't want To hit es. MovingImage.Left = MovingLeft MovingImage.Top = MovingTop |