Search code examples
vb62dplatform

Vb6 Moving Platforms in a 2d side scroller


Ok so I have developed a 2d side scroller (platformer) and it is pretty darn efficient IMO, uses 1 timer, and im trying to make moving platforms possible. Here is what i tried and am debugging with

 Private Sub Timer1_Timer()
'moving platforms
For f = 0 To Platform.Count - 1
    If Platform(f).Tag = "moving" Then
        For j = 0 To Platform.Count - 1
            If Collision(Platform(f), Platform(j)) And j <> f Then
                Speed(f) = Speed(f) * -1
            End If
        Next j
        Platform(f).Left = Platform(f).Left + Speed(f)
    End If
Next f

Basically, here iswhat the code does: For all the platforms, it checks which platform has the tag "moving", if it does have that tag, move it, but before moving it, see if it needs to have a direction change, so it loops through all the platforms again to see if it needs to change again, and should do so if it needs to, but in this code it doesnt work :(

What could be the problem? The intial value of all the Speeds is 1, and I have scalemode to pixel, thats why its so small. Any help is appreciated

Collision function:

Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
If (Shape1.Left + Shape1.Width) > Shape2.Left _
And Shape1.Left < (Shape2.Left + Shape2.Width) _
And (Shape1.Top + Shape1.Height) > Shape2.Top _
And (Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height Then
    Collision = True
Else
    Collision = False
End If
End Function

Solution

  • Alrighty, the problem has been solved. Observe this Collision function:

    Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
        If (Shape1.Left + Shape1.Width) > Shape2.Left _
        And Shape1.Left < (Shape2.Left + Shape2.Width) _
        And (Shape1.Top + Shape1.Height) > Shape2.Top _
        And (Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height Then
            Collision = True
        Else
            Collision = False
        End If
    End Function
    

    Notice the problem? The 2 platforms I am testing for collision for, in this case, are at the same height. Think about it, if you see in that collision function, the last condition:

    (Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height
    

    Since Shape1 has the same Top as Shape2, this condition is false because I sued the less than sign, and not the less than or EQUAL to sign. To make sure there is no problem, I replaced all my signs with or equal to ones.

    Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
        If (Shape1.Left + Shape1.Width) >= Shape2.Left _
        And Shape1.Left <= (Shape2.Left + Shape2.Width) _
        And (Shape1.Top + Shape1.Height) >= Shape2.Top _
        And (Shape1.Top + Shape1.Height) <= Shape2.Top + Shape2.Height Then
            Collision = True
        Else
            Collision = False
        End If
    

    End Function

    Would you look at that! A moving platform that bounces between the platforms beside it. Beautiful.