# Need a VB overlap function.



## CtrlAltDstry (May 10, 2003)

Hey, is there a Visual Basic function that returns true if 2 controls overlap? Can I have it?


----------



## AbvAvgUser (Oct 3, 2002)

Your question doesn't make sense to me. Can you please be more clear?


----------



## CtrlAltDstry (May 10, 2003)

Ok, say I'm making a game that involves moving image boxes around on a form. I want to check to see if 2 of the image boxes overlap (touch) each other. I need a function that accepts the 2 image boxes that I want to check. It would return TRUE if they do, and FALSE if they don't. I want to be able to do something like this:

If Overlap(imgShip, imgEnemy) Then
TakeDamage
BounceOff
End If

Make more sense?


----------



## Mosaic1 (Aug 17, 2001)

No function I know of. you have to do it yourself, I think.
I wrote this Sub. You could easily convert it to a function. I used two Images and a command button. Moving the images around to test. If Overlap is true, they overlap.


```
Private Sub Command1_Click()
 'Check to see if two image boxes overlap
Dim Overlap As Boolean
Dim sideOverlap As Boolean, topOverlap As Boolean

 If Image1.Left < Image2.Left And _
  Image2.Left <= Image1.Left + Image1.Width Then
    sideOverlap = True
   ElseIf Image2.Left < Image1.Left And _
     Image1.Left <= Image2.Left + Image2.Width Then
      sideOverlap = True
 End If
If Image1.Top < Image2.Top And _
  Image2.Top <= Image1.Top + Image1.Height Then
    topOverlap = True
    ElseIf Image2.Top < Image1.Top And _
  Image1.Top <= Image2.Top + Image2.Height Then
    topOverlap = True
End If

 If topOverlap And sideOverlap Then Overlap = True

     MsgBox Overlap & " " & "Overlap" 'returns true or false

      'Bounce or whatever you like.
      'the images overlap if true and also, if touching,
      'overlap is true. 
End Sub
```
 Edit: Forgot one = sign. Now they'll all show as true if any edge is touching.


----------

