character doesnt always jump when on platform
Blitz3D Forums/Blitz3D Beginners Area/character doesnt always jump when on platform
| ||
Ok. My character doesnt always jump when hes on a platform. I think it has something to do with thr jumpheight fluctating, and i dont know how to make it stop without the whole routine not working.
Graphics 800,600
SetBuffer BackBuffer()
Global dude = LoadImage("dude.png")
Global dline = LoadImage("dudeline.png")
Global pltf = LoadImage("platform.png")
Global pltfl = LoadImage("platline.png")
Global jumpheight# = 10
Global canjump = 0
Global jump = 0
Const gravity# = 0.4
Type player
Field x#
Field y#
Field h#
End Type
Type platform
Field x
Field y
Field g
End Type
createplatform()
createplayer()
While Not KeyHit ( 1 )
Cls
Text 50,50,"Jumpheight = " +jumpheight
drawlines()
renderplatform()
renderplayer()
moveplayer()
playerjump()
collisionchecks()
Flip
Wend
End
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Draw Lines ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function drawlines()
For p.player = Each player
DrawImage dline,p\x + 3,p\y + 50
Next
For plat.platform = Each platform
DrawImage pltfl,plat\x,plat\y - 1
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplatform()
plat.platform = New platform
plat\x = 600
plat\y = 535
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplayer()
p.player = New player
p\x = 300
p\y = 540
p\h = 10
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Render Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplayer()
For p.player = Each player
DrawImage dude,p\x,p\y,0
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function :Render Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplatform()
For plat.platform = Each platform
DrawImage pltf,plat\x,plat\y
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Move Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function moveplayer()
If KeyDown ( 203 )
For p.player = Each player
p\x = p\x - 3
Next
ElseIf KeyDown ( 205 )
For p.player = Each player
p\x = p\x + 3
Next
EndIf
For p.player = Each player
If p\y > 540 Then
p\y = 540
canjump = 0
jump = 0
jumpheight = 10
EndIf
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Player Jump ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function playerjump()
If KeyHit ( 57 ) And (jumpheight = 10 Or (Int(jumpheight = -1)))
canjump = 1
jump = 1
jumpheight = 10
EndIf
If canjump = 1 And jump = 1 Then
If jumpheight <=0 Then
For p.player = Each player
For i = 0 To 1 + Abs(jumpheight)
If collisionchecks() = True Then Exit
p\y = p\y + 1
Next
Next
Else
For p.player = Each player
For i = 0 To jumpheight
If collisionchecks = True Then Exit
p\y = p\y - 1
Next
Next
EndIf
jumpheight = jumpheight - gravity
Else
jump = 0
canjump = 0
jumpheight = 10
EndIf
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Collision checks ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function collisionchecks()
For p.player = Each player
For plat.platform = Each platform
If ImagesCollide(dline,p\x + 3,p\y + 50,0,pltfl,plat\x,plat\y - 1,0) And jumpheight <0 Then
canjump = 1
jump = 0
Return True
ElseIf p\y <540 And jumpheight = 10 Then
canjump = 1
jump = 1
jumpheight = -1
Return True
EndIf
Next
Next
Return False
End Function
|
| ||
maybeIf KeyHit(57) And (jumpheight = 10 Or Int(jumpheight) = -1) confusing brackets there, maybe? |
| ||
Try this:Graphics 800,600
SetBuffer BackBuffer()
Global dude = LoadImage("dude.png")
Global pltf = LoadImage("platform.png")
Const gravity# = 0.4
Type player
Field x#
Field y#
Field h#
Field Yspeed# ; --* ADDED ADDITIONAL VALUE *--
End Type
Type platform
Field x
Field y
Field g
End Type
createplatform()
createplayer()
While Not KeyHit ( 1 )
Cls
p.player = First player
Text 50,50,"Player Y speed = " + p\Yspeed#
renderplatform()
renderplayer()
moveplayer()
playerjump()
Flip
Wend
End
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplatform()
plat.platform = New platform
plat\x = 600
plat\y = 535
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Create Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function createplayer()
p.player = New player
p\x = 600
p\y = 400
p\h = 10
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Render Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplayer()
For p.player = Each player
DrawImage dude,p\x,p\y,0
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function :Render Platform ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function renderplatform()
For plat.platform = Each platform
DrawImage pltf,plat\x,plat\y
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Move Player ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function moveplayer()
If KeyDown ( 203 )
For p.player = Each player
p\x = p\x - 3
Next
ElseIf KeyDown ( 205 )
For p.player = Each player
p\x = p\x + 3
Next
EndIf
For p.player = Each player
If p\y > 540 Then
p\y = 540
canjump = 0
jump = 0
jumpheight = 10
EndIf
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Player Jump ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function playerjump()
If KeyHit ( 57 )
; ------------------------------------
; ADDED VERTICAL SPEED CONTROL CODE
; ------------------------------------
For p.player = Each player
If p\Yspeed# = gravity#
p\Yspeed# = -5.5 ; USE A MINUS VALUE TO MOVE UPSCREEN
EndIf
Next
EndIf
For p.player = Each player
If p\Yspeed <> 0
If collisioncheck( p\x, p\y + p\Yspeed# )
If p\Yspeed > 0
p\Yspeed# = 0 ; STOP PLAYER IF FALLING
Else
p\Yspeed# = 0.1 ; MAKE PLAYER FALL IF HIT ROOF
EndIf
Else
p\y = p\y + p\Yspeed#
EndIf
EndIf
p\Yspeed# = p\Yspeed# + gravity#
Next
End Function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Function : Collision checks ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function collisioncheck(x,y)
For p.player = Each player
For plat.platform = Each platform
If ImagesCollide( dude, x, y, 0, pltf, plat\x, plat\y, 0)
Return True
EndIf
Next
Next
Return False
Return False
End FunctionNotice that this does away with the lines and deals directly with the character. Also I have replaced jumpheight et al with Yspeed#. Also notice that gravity is always applied to the character, pulling him towards the ground. You'll have to make a seperate check for X collisions, but you can use the same collision function for it, just change the x value instead of Y. |