Page 1 of 1
Sidescroller jumping
Posted: Thu Sep 20, 2007 3:04 pm
by Mathias
Hi,
Do you have any advice as on how to make a character in a sidescrolling game jump?
I have made the player character collide with a tilemap, but I don't have a clue on how to make him jump off the ground, and then
be forced back on the ground by gravity.
I have been twisting my head around this problem for quite a while now, and I was hoping you could help me out.
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 3:40 pm
by Pie2
It's pretty simple. You just add gravity to the players y-speed. And when you want to jump you just set the y-speed to a value in the opposite direction.
Code: Select all
//LOADING etc.
//MISC
Const gravity = 0.2
Global y_speed As Float
//PLAYER
player=LoadObject("media/guy.bmp")
//FLOOR
//MAKE THE IMAGE
floor_img=MakeImage(400,50)
DrawToImage floor_img
Color 200,200,200
Box 0,0,400,50
DrawToScreen
//MAKE THE FLOOR OBJECT
floor=MakeObject()
PaintObject floor,-floor_img
//PLACE THE OBJECTS
//FLOOR
PositionObject floor,0,-130
//SET THE COLLISION SYSTEM
SetupCollision player,floor,1,1,2
//GAME LOOP
Repeat
//CHECK INPUT
If LeftKey() Then TranslateObject player,-2,0
If RightKey() Then TranslateObject player,2,0
If UpKey() And onfloor Then y_speed=7
//UPDATE
//GRAVITY
If onfloor=0 Then y_speed=y_speed-gravity
//PLAYER
//MOVEMENT
TranslateObject player,0,y_speed
//COLLISION
If ObjectsOverlap(player,floor,3) Then
//STOP MOVEMENT
y_speed=0
//UPDATE THE onfloor-VARIABLE
onfloor=1
Else
//UPDATE THE onfloor-VARIABLE
onfloor=0
EndIf
//DRAW ON SCREEN
DrawScreen
Until EscapeKey()
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 3:42 pm
by Buckethead
I'll use a cos/sin function.
20*Sin(t) is a vertical jump of 20 pixels, t is 0 to 180° otherwise there's a problem with the sign. You can add the speed on X with speed = speed -1 until it's 0 or let the keys makes this work.
Code: Select all
SCREEN 640,480
Text 0,0, "Press any key to jump..."
Dot 320,240
WaitKey
For t = 0 To 180 Step 10
Dot 320, 240-20*Sin(t)
DrawScreen
Next t
WaitKey
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 3:53 pm
by Mathias
Alright, thanks for the help guys. I think I've got it now,
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 4:01 pm
by koodaaja
Buckethead wrote:I'll use a cos/sin function.
20*Sin(t) is a vertical jump of 20 pixels, t is 0 to 180° otherwise there's a problem with the sign. You can add the speed on X with speed = speed -1 until it's 0 or let the keys makes this work.
Code: Select all
SCREEN 640,480
Text 0,0, "Press any key to jump..."
Dot 320,240
WaitKey
For t = 0 To 180 Step 10
Dot 320, 240-20*Sin(t)
DrawScreen
Next t
WaitKey
But try to do that with multiple platforms at different heigths ;D
My advice: press F8 and select "19: painovoima". And that's it. Comments are in finnish, but try to understand the code itself =D
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 4:09 pm
by Mathias
Yes, it does work with objects, but what if i want to make it work with a tilemap? If i set:
i only get a integer divide by zero error.
Re: Sidescroller jumping
Posted: Thu Sep 20, 2007 4:11 pm
by Mathias
Didn't see the above comment.
I'll look into that tutorial chapter.