Godmil
Active Member
Hello, I've only recently started to learn how to code, and I'm totally stuck on something that seems very simple. I've been following the Lazy Foo tutorials and decided to adapt this one to give the moving ball some gravity and get it to jump (in a Manic Miner sort of way - so no directional controls mid air).
It was going well, but if you take your fingers off the keys while the ball is in mid air, when it lands it will keep moving in the direction it was going in (until it hits a wall or you press another key). I cant for the life of me undersand why, could someone help please?
Here are the main bits of code:
I have a few int constants (MOVINGVEL, JUMPINGVEL, and GRAVITY), and 'jumping' is a bool set in the dot class.
It's like the 'jumping=false' near the bottom isn't being picked up by the conditional near the top, unless another button is pushed first.
exe and full program attached here (edit: had to remove full program, goes against Lazy Foo's copyright)
I'd be soo greatful if anyone could help me, I'd hate to lose my motivation due to something so small
Thanks,
Godmil
It was going well, but if you take your fingers off the keys while the ball is in mid air, when it lands it will keep moving in the direction it was going in (until it hits a wall or you press another key). I cant for the life of me undersand why, could someone help please?
Here are the main bits of code:
I have a few int constants (MOVINGVEL, JUMPINGVEL, and GRAVITY), and 'jumping' is a bool set in the dot class.
Code:
void Dot::handle_input()
{
// Stops the dot moving when no keys are pressed
if (jumping == false)
{
yVel=0;
xVel=0;
}
//initialise keystates
Uint8 *keystates = SDL_GetKeyState( NULL );
if ( keystates[ SDLK_LEFT ] && jumping == false)
{
xVel=-MOVINGVEL;
}
if ( keystates[ SDLK_RIGHT ] && jumping == false)
{
xVel=MOVINGVEL;
}
if ( keystates[ SDLK_UP ] && jumping == false)
{
yVel=-JUMPINGVEL;
jumping = true;
}
}
void Dot::show()
{
//Move the dot left or right
x += xVel;
//If the dot went too far to the left or right
if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
{
//move back
x -= xVel;
}
//Move the dot up or down
y += yVel;
if (jumping == true)
{
yVel += GRAVITY;
}
//If the dot went too far up or down
if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
if (jumping == true)
{
y -= (yVel - GRAVITY);
}
else y -= yVel;
jumping = false;
}
//Show the dot
apply_surface( x, y, dot, screen );
}
It's like the 'jumping=false' near the bottom isn't being picked up by the conditional near the top, unless another button is pushed first.
exe and full program attached here (edit: had to remove full program, goes against Lazy Foo's copyright)
I'd be soo greatful if anyone could help me, I'd hate to lose my motivation due to something so small
Thanks,
Godmil