Hello again! I am trying to make a jump and run game using SDL, but I am stuck at the jumping part. Whenever I press the Up key, the sprite should go up. However, when I press it, it goes down. I have changed it and done math that should make it go up, but it does nothing. I have also a loop that while jumping, decreases the value of the variables for gravity. But if I have it set up as a loop (I use the "while" command), nothing happens. But if I change "while" to "if", the sprite jumps! Unfortunately, it only runs the math for my "gravity" once and every button pressed after that increases the Y value. (Which is weird). I know this is once again something small that I missed, or maybe I did it wrong. Please help. Here is my code:
Wolf::Wolf()
{
offSet = 0;
velocity = 0;
posy = 275;
vely = 0;
accel = 10;
jump = false;
frame = 0;
status = WOLF_RIGHT;
}
void Wolf::get_keystate()
{
Uint8 *keystates = SDL_GetKeyState( NULL );
if( event.type == SDL_KEYDOWN )
{
if( keystates [SDLK_RIGHT] )
{
velocity += WOLF_WIDTH / 4;
}
if( keystates [SDLK_LEFT] )
{
velocity -= WOLF_WIDTH / 4;
}
if( keystates [SDLK_UP] )
{
vely = 100;
posy = posy - vely;
jump = true;
}
while( jump == true )
{
posy = posy - vely;
vely = vely - 10;
if( posy > 275 )
{
vely = 0;
posy = 275;
jump = false;
break;
}
}
}
if( event.type == SDL_KEYUP )
{
velocity = 0;
}
}