[SDL] Jumping input

Started by
12 comments, last by _paf 11 years, 2 months ago

First delete this section


if (jumping == true)
{
myDot.yVel -= 120;
if (myDot.yVel < -240)
{

myDot.yVel = 240;
myDot.yVel = 0;
jumping = false;
}
}

Then go to your Dot class, add a "int gravity" and a "int maxY" variables.

On the Dot class constructor, initalize the gravity to, for example 10, and the maxY to 240.

Now on the Dot::move method, insert the following:


  //Only execute if the box is jumping
  if(jumping)
  {

    //If the box is falling at the maximum speed (maxY serves as the maximum up speed, as well as maximum down speed)
    if(yVel - gravity < -maxY)
    yVel = -maxY;  //Don't increment yVel, instead make it fall at exactly the maximum speed
    //Else, the box as yet to reach the maximum downfall speed, so add the gravity to the falling speed
    else
    yVel -= gravity;

  //Update the box's y position every frame
  Box.y += yVel;

    //Check if the box hit the floor
    if( (box.y < 0) || (box.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) )
    jumping = false;  //Disable jumping

  }

and lastly in


case SDLK_UP: 
if (!jumping)
{
  jumping = true;
    yVel = 0;
} break;

set yVel to maxY instead.

NOTE: Your code seems to have the Y values inverted (top is 0, bottom is window_height), if it is so, then invert the signs of the jumping in the move function.

See if it works.

Advertisement

i did everything you asked but when i press space the program crashes with no error report.

and when you look at my debugging for the yVel it goes 0,0,0,0,0,0,0,0,230(pressed the space) and then crashed

bump

Did you try changing the sign?

Change


//Only execute if the box is jumping
  if(jumping)
  {
 
    //If the box is falling at the maximum speed (maxY serves as the maximum up speed, as well as maximum down speed)
    if(yVel - gravity < -maxY)
    yVel = -maxY;  //Don't increment yVel, instead make it fall at exactly the maximum speed
    //Else, the box as yet to reach the maximum downfall speed, so add the gravity to the falling speed
    else
    yVel -= gravity;
 
  //Update the box's y position every frame
  Box.y += yVel;
 
    //Check if the box hit the floor
    if( (box.y < 0) || (box.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) )
    jumping = false;  //Disable jumping
 
  }

to


//Only execute if the box is jumping
  if(jumping)
  {
 
    //If the box is falling at the maximum speed (maxY serves as the maximum up speed, as well as maximum down speed)
    if(yVel + gravity > maxY)
    yVel = maxY;  //Don't increment yVel, instead make it fall at exactly the maximum speed
    //Else, the box as yet to reach the maximum downfall speed, so add the gravity to the falling speed
    else
    yVel += gravity;
 
  //Update the box's y position every frame
  Box.y += yVel;
 
    //Check if the box hit the floor
    if( (box.y < 0) || (box.y + DOT_HEIGHT > LEVEL_HEIGHT ) || touches_wall( box, tiles ) )
    jumping = false;  //Disable jumping
 
  }

and lastly in


case SDLK_UP: 
if (!jumping)
{
  jumping = true;
    yVel = -maxY; //Set iVel to -maxY here instead of 0
} break;

See if it works now, if you didn't try it before.

This topic is closed to new replies.

Advertisement