I am trying to do a game where a player lands on a platform but can't seem to get the player to collide with the platform or the platform at the base of the form. The player just goes straight through. I have used a Collision module and below is the code I have but it doesn't seem to be working. Any thoughts appreciated.
PublicClass Form1
Dim tfLeft As Boolean = Fals
Dim tfRight As Boolean = False
Dim objBit(1) As Bitmap
Dim LeapDist As Integer ' alternates between +4 and -4
Const BaseTop As Integer = 286 ' Top of character at base level
Dim PlatformTop As Integer = 181 ' Top of character at platform level
Const LeapTop As Integer = 34
Dim CurrentState As Movement = Movement.Horizontal
Private Enum Movement
Leaping = 0
Returning = 1
Falling = 2
Horizontal = 3
End Enum
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
tfLeft = true
picStick.Left -= 5
End If
If e.KeyCode = Keys.Right Then
tfRight = True
picStick.Left += 5
End If
If e.KeyCode = Keys.Space Then
CurrentState = Movement.Leaping
LeapDist = -4 ' on way up
tmrLeap.Start()
End If
End Sub
Private Sub tmrLeap_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrLeap.Tick
picStick.Top += LeapDist ' move up/down
End Sub
Private Sub picstick_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles picStick.LocationChanged
Select Case CurrentState
Case Movement.Returning ' returning back down from a leap
'if character has hit the bottom or the platform
If picStick.Top = BaseTop Or (Collision(picStick, picPlatform2) And picStick.Top = PlatformTop) Then
CurrentState = Movement.Horizontal
tmrLeap.Stop()
End If
Case Movement.Falling ' falling after dropping from platform
If picStick.Top = BaseTop Then
CurrentState = Movement.Horizontal
tmrLeap.Stop()
End If
Case Movement.Leaping ' leaping upwards
'if character has reached top of leap
If picStick.Top <= LeapTop Then
LeapDist = 4
CurrentState = Movement.Returning
ElseIf Collision(picStick, picPlatform2) Then
LeapDist = 4
CurrentState = Movement.Falling
End If
Case Movement.Horizontal
If picStick.Top = PlatformTop And Not Collision(picStick, picPlatform2) Then
LeapDist = 4
CurrentState = Movement.Falling
tmrLeap.Start()
End If
End Select
End Sub
Private Sub tmrWalk_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWalk.Tick
Static whichimage As Integer = 0
If tfRight = True Then
whichimage += 1
whichimage = whichimage Mod 8
picStick.Image = imlWalking.Images.Item(whichimage)
tfRight = False
End If
End Sub
End Class
PublicClass Form1
Dim tfLeft As Boolean = Fals
Dim tfRight As Boolean = False
Dim objBit(1) As Bitmap
Dim LeapDist As Integer ' alternates between +4 and -4
Const BaseTop As Integer = 286 ' Top of character at base level
Dim PlatformTop As Integer = 181 ' Top of character at platform level
Const LeapTop As Integer = 34
Dim CurrentState As Movement = Movement.Horizontal
Private Enum Movement
Leaping = 0
Returning = 1
Falling = 2
Horizontal = 3
End Enum
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Left Then
tfLeft = true
picStick.Left -= 5
End If
If e.KeyCode = Keys.Right Then
tfRight = True
picStick.Left += 5
End If
If e.KeyCode = Keys.Space Then
CurrentState = Movement.Leaping
LeapDist = -4 ' on way up
tmrLeap.Start()
End If
End Sub
Private Sub tmrLeap_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrLeap.Tick
picStick.Top += LeapDist ' move up/down
End Sub
Private Sub picstick_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles picStick.LocationChanged
Select Case CurrentState
Case Movement.Returning ' returning back down from a leap
'if character has hit the bottom or the platform
If picStick.Top = BaseTop Or (Collision(picStick, picPlatform2) And picStick.Top = PlatformTop) Then
CurrentState = Movement.Horizontal
tmrLeap.Stop()
End If
Case Movement.Falling ' falling after dropping from platform
If picStick.Top = BaseTop Then
CurrentState = Movement.Horizontal
tmrLeap.Stop()
End If
Case Movement.Leaping ' leaping upwards
'if character has reached top of leap
If picStick.Top <= LeapTop Then
LeapDist = 4
CurrentState = Movement.Returning
ElseIf Collision(picStick, picPlatform2) Then
LeapDist = 4
CurrentState = Movement.Falling
End If
Case Movement.Horizontal
If picStick.Top = PlatformTop And Not Collision(picStick, picPlatform2) Then
LeapDist = 4
CurrentState = Movement.Falling
tmrLeap.Start()
End If
End Select
End Sub
Private Sub tmrWalk_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWalk.Tick
Static whichimage As Integer = 0
If tfRight = True Then
whichimage += 1
whichimage = whichimage Mod 8
picStick.Image = imlWalking.Images.Item(whichimage)
tfRight = False
End If
End Sub
End Class