SDL C++ Jumping -


const int jumpheight             = 10;  //non-const variables bool running                     = true; bool isjumping                   = false; bool isfalling                   = false; int jump                         = 0; uint8 *keystate                  = null;  //structs typedef struct entity {     sdl_rect hitbox; } playertype, enemytype; playertype player; enemytype basicenemy[10];  //main function int main( int argc, char* argv[] ) {     while( running )     {             keystate = sdl_getkeystate(null);             if( keystate[sdlk_up] )             {                     if(isjumping != true)                     {                             isjumping = true;                     }             }             if( keystate[sdlk_left] )  player.hitbox.x -= 1;             if( keystate[sdlk_right] ) player.hitbox.y += 1;             //window collision             if( player.hitbox.x < 0 ) {player.hitbox.x  = 0;}             else if( player.hitbox.x > screen_width  - player.hitbox.w ) {player.hitbox.x = screen_width  - player.hitbox.w;}             if( player.hitbox.y < 0 ) {player.hitbox.y  = 0;}             else if( player.hitbox.y > screen_height - player.hitbox.h ) {player.hitbox.y = screen_height - player.hitbox.h;}              //jumping             if( isjumping == true )             {                     if( jump >= jumpheight || isfalling == true )                     {                             jump--;                             player.hitbox.y--;                             isfalling = true;                     }else if( jump <= 0 && isfalling == true )                     {                             jump - 0;                             isfalling = false;                             isjumping = false;                     }else {                             jump++;                             player.hitbox.y++;                     }             }     } } 

this current code in game (the parts related jumping anyway). when press key character goes top of window , stays there. have gone wrong? compile g++ -o myprogram.exe mysource.cpp -lmingw32 -lsdlmain -lsdl -lsdl_image -static-libgcc -static-libstdc++. walking through how code supposed work: press up: isjumping becomes true, , player raised max height, goes down again started (later add collision checking)

he's going top of window because of code here:

if (jump >= jumpheight || isfalling == true) {     jump--;     player.hitbox.y--;     isfalling = true; } 

once condition has been met, run rest of loop , conditions never false. fix it, need check if jump 0 after decrement it. if it's 0, set isfalling false , isjumping false.

also, note when increase y, you're making unit go lower on window because sdl windows have origins starting top left corner , y axis downward.

replace increments of y decrements , vice versa.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -