Sidescroller jumping

Post your coding questions such as 'how to' here.
Post Reply
Mathias

Sidescroller jumping

Post 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.
Pie2
Member
Posts: 55
Joined: Mon Aug 27, 2007 8:14 pm
Location: Porvoo

Re: Sidescroller jumping

Post 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()
Since 24-March 05
Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: Sidescroller jumping

Post 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
Mathias

Re: Sidescroller jumping

Post by Mathias »

Alright, thanks for the help guys. I think I've got it now,
koodaaja
Moderator
Moderator
Posts: 1583
Joined: Mon Aug 27, 2007 11:24 pm
Location: Otaniemi - Mikkeli -pendelöinti

Re: Sidescroller jumping

Post 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
Mathias

Re: Sidescroller jumping

Post by Mathias »

Yes, it does work with objects, but what if i want to make it work with a tilemap? If i set:

Code: Select all

 If ObjectsOverlap(player,map,3)
i only get a integer divide by zero error.
Mathias

Re: Sidescroller jumping

Post by Mathias »

Didn't see the above comment. :P I'll look into that tutorial chapter.
Post Reply