Jumping in one direction

BlitzPlus Forums/BlitzPlus Beginners Area/Jumping in one direction

loonix(Posted 2015) [#1]
Hello all......

I have a little platform game im trying my hand at
and im looking at a couple of head scratchers
1st, you are moving left or right and you need to jump
ive got all that done and sorted but when your player is in mid air you can still change direction,,, i wanted to just be committed to jumping in the direction the player started off in..... been trying all sorts of checking flags ... tna

also im using an array to plot the different objects on the screen but im having an issue with landing on different ground surfaces when the player jumps on them just keeps going back to the original y pos

any help would be very much appreciated


Midimaster(Posted 2015) [#2]
jumping:

Create a new variable IS_JUMPING. In the moment when the jump starts, set it to TRUE and when the player is landed set it to FALSE.

In the code, where you check the joystick or keys for left/right add and check of this variable:
If KeyHit(200)
	' jump starts
	y=y+1
	....
	IS_JUMPING=TRUE
Endif

If KeyHit(203)
	' left
	If IS_JUMPING=FALSE
		x=x-1
	....
	Endif
Endif


If KeyHit(205)
	' right
	If IS_JUMPING=FALSE
		x=x+1
	....
	Endif
Endif




RemiD(Posted 2015) [#3]
Another way would be to check the "PositionState" (OnGround or OnAir) of the character at the beginning of the loop, then if character is OnGround, he can move and jump (here calculate the movement vector), but if character is OnAir, he can not move or jump, he is only translated by the previously calculated movement vector.


loonix(Posted 2015) [#4]
Hi ,,, thx for the replys..... it may help if you had a look at the code ...... comments in there should explain ish a bit better


loonix(Posted 2015) [#5]

; 32 x 32 sprites
;set graphics mode

Graphics 640,480,16,2


;load sprite images and mask

imgobj=LoadAnimImage("sprite.png",32,32,0,144)

MaskImage imgobj,0,0,0

;create game timer

timer = CreateTimer(15)

;set starting frame for animation

frame = 0


;set some vars

x# = 100
y# = 100
Gravity#=.1
gy# = y
onground = True
jleft = False		;was playing with these
jright = False 		;true false flags to see if i could stop the movement left / right in the air


;set drawing buffer to back buffer
SetBuffer BackBuffer()

;start main while loop
While Not KeyHit(1)
	
	;clear screen
	Cls
	
	;draw a couple of lines for char' to walk/jump on
	 
	Line 0,100,64,100
	Line 0,132,640,132
	
		
	;just a load of vars i had on the screen to see what they were up to as i messed about trying different things
	
	Text 0,345,"frame = " + frame
	Text 0,365,"jleft = "+jleft
	Text 0,385,"jright = "+jright
	Text 100,0,"y = " + y
	Text 200,0,"gy = " + gy
	Text 0,0,"x = " + x
		
	;draw images
	DrawImage imgobj,x,y,frame
		

	;jump and gravity stuff
	
	y = y + gravity	
	
	If y>gy Then 						;gy was one of the vars/flags i was messin with to try and stop movement left / right whilst in the air
		y=gy							;also want players y value to change if player happens to have new level of ground below his feet,
		Gravity=0						;not figured that bit out yet !
		onground = True
		
	Else
		Gravity = Gravity + .5
	EndIf
	
	
	If KeyHit(57) And onground = True	;check for double jump --- as dont want it. This works ok !! :)
		Gravity=-6.5
		onground = False
	EndIf	
		
	; left and right movement
	
	If KeyDown(205) ;And onground = True ; <----------- if this is uncommented,(and/or next if statement for left key), as soon as you jump the char' stops moving in the direction 
			frame = frame + 1			;			  he started off in , and just goes straight up and down untill he hits ground again					
		If frame >= 4					;			   I want the char to carry on jumping in the direction he was going in when jump is pressed	
			frame = 0					;			  and not move right / left in the air.
 			x = x + 16					;			  Also if you let go of the right or left key whilst in the air the char' 
		EndIf							;			  stops moving in the dir' he started off in and just falls down vertically
	EndIf
			
	
	If KeyDown(203) ;And onground = True
		frame = frame - 1
		If frame < 4	
		frame = 7
		x = x - 16
		EndIf
	EndIf
	
	;checks if sprite x cord is no at edge of screen.... if so then reset x cord and frame	
		
	
	If x >= 612
		x = 612
		frame = 0
	EndIf
	
	
	If x <= 4 
		x = 4
		frame = 7
	EndIf
	
	;wait for game timer
	WaitTimer timer
	
	
	
	;flip buffers to show image on screen
	Flip
	
;end main while loop
Wend
;end program
End




Midimaster(Posted 2015) [#6]
you have to separate the checking of the keys from adding the movement:

Global LEFT=1, RIGHT=2
.....
If KeyDown(203) And onground = TRUE
	CurrDir=LEFT
ElseIf KeyDown(205) And onground = TRUE
	CurrDir=RIGHT
ElseIf KeyDown(200) And onground = TRUE
	onground = FALSE
ElseIf onground = TRUE
	CurrDir=0
Endif
....
If CurrDir=LEFT
	frame = frame - 1
	If frame < 4	
	frame = 7
	x = x - 16
EndIf
....



loonix(Posted 2015) [#7]
ohh i see , nice
thank you very much