character doesnt always jump when on platform

Blitz3D Forums/Blitz3D Beginners Area/character doesnt always jump when on platform

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 Function
Notice 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.