Results 1 to 4 of 4

Thread: is there a way to see if these 2 shapes are touching

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    hello all, I'm making this game where food is flying a person, and I want to make it so that when the food touches the person, the food desappears, and a point is added to the score this is the code that I have so far:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyLeft:     Shape1.Left = Shape1.Left - 100
        Case vbKeyRight:    Shape1.Left = Shape1.Left + 100
    End Select
    End Sub
    
    Private Sub Form_Load()
    Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
    Shape2.Top = Shape2.Top + 10
    End Sub
    for now I'm just testing everything with shapes, then I'm gonna replace the shapes with images
    NXSupport - Your one-stop source for computer help

  2. #2
    Guest
    How about the IntersectRect API?
    Code:
    Public Declare Function IntersectRect Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
    Public Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Code:
    Private Sub Timer1_Timer()
    
        Dim rctShape As RECT
        Dim rctPerson As RECT
        Dim rctResult As RECT
    
        Shape2.Top = Shape2.Top + 10
    
        With rctShape
            .Left = Shape2.Left
            .Top = Shape2.Top
            .Right = Shape2.Left + Shape2.Width
            .Bottom = Shape2.Top + Shape2.Height
        End With
    
        'The same way fill the RECT of the person (rctPerson).
    
        If (IntersectRect(rctShape, rctPerson, rctResult) <> 0) Then
            'They intersect.
        End If
        
    End Sub
    Let me know.

  3. #3
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    You can also use this code:
    Code:
    if shape2.top + shape2.height=> shape1.top then
    	if (shape2.left<(shape1.left+shape1.width)) _
    	and shape2.left>(shape1.left) then debug.print "Hit."
    end if
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    ok, thanks to both
    NXSupport - Your one-stop source for computer help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width