NOTE:
Apparently, in libgdx, the origin is at the lower left corner of the
screen. You should switch "IncreaseYCoordinate" with
"DecreaseYCoordinate" in your actual code.
Jumping, at the base, is language and framework agnostic. You should look at it that way if you wish to learn anything.
"Jumping", especially in 2D games, is the process of modifying a character's (sprite's) position on the screen so that he or she appears to be jumping. That's all. You just draw your character at a different height on each Draw call, so it appears to be moving vertically (jumping).
I'm not exactly familiar with libgdx, but I presume you're doing something like:
batch.draw(someTexture, 100, 350);
Where 100 is the X coordinate and 350 the Y coordinate of the drawn texture. This obviously makes your texture show up at the position described by the vector (100, 350). I am currently supposing that the origin (0,0) is the upper left corner of the screen (increasing the Y coordinate makes your sprite show up lower on the screen).
Now, if you were to decrease the Y coordinate, like:
batch.draw(someTexture, 100, 349);
Your texture would get drawn at a higher position on the screen.
batch.draw(someTexture, 100, 348);
And even higher, now.
Simple, is it not? The first thing you need to do is to get your sprite to continuously ascend until he should stop doing so. Then, the sprite should go back to its original position by descending.
Since you have not posted any actual code, and the question is a bit broad, I can only give some rough guidelines.
- Store your sprite's position in a Vector-like structure. Or two variables, one for the X coordinate, and one for the Y coordinate.
- The jump should go smoothly. Keep two variables: "speed" and "originalSpeed". These should be integers;
- Keep a state flag, that tells the Update() function when to increase or decrease the sprite's Y coordinate. This can be an enum with the values { Ascending, Descending, Standing }
- Intercept the touch event, and when it happens, set the state flag to Ascending (if it's not already set to Ascending);
In the Update method, you should change the Y coordinate of the sprite considering its current state. Thus:
if (State == Ascending)
{
//We're ascending. Decrease the Y coordinate of the sprite by the speed.
DecreaseYCoordinateBy(Speed);
DecreaseByValue(Speed, Value); //The character needs to jump smoothly, so the speed should decrease as he ascends.
if (Speed <= 0)
{
ChangeState(Descending); //If speed is <= 0, then the character should fall down.
Speed = 0;
}
}
else if (State == Descending)
{
//We're descending. Increase the Y coordinate by the speed (at first, it's 0).
IncreaseYCoordinateBy(Speed);
IncreaseByValue(Speed, Value); //Increase the speed, so the character falls gradually faster.
if (CurrentYCoordinate >= OriginalYCoordinate)
{
//If we reached the original Y coordinate, we hit the ground. Mark the character as standing.
ChangeState(Standing);
CurrentYCoordinate = OriginalYCoordinate;
Speed = OriginalSpeed;
}
}
Drawing is done like:
batch.draw(yourSprite, CurrentXCoordinate, CurrentYCoordinate);
Obviously, you need to enter your own values for the original coordinates and speeds. Tweak them to find the values that best suit your needs. This is not exactly the "best" way to do it, but should give you a starting point. You may want to impose some limitations on the falling speed, so that it increases only up to a certain amount. You could (and should) use different speeds for jumping and falling too.
Also, this is not libgdx related, but might give you a few hints:
http://www.xnadevelopment.com/tutorials/thewizardjumping/thewizardjumping.shtml
I definitely recommend that you search for as many such tutorials as possible, even if they're not libgdx related. Jumping is a concept that can be learned from just about any source.