Rev0Lucian: XNA Adding Double Jump

So last night I was exploring the code in the Platformer Project in XNA studio, and I decided I wanted to add a double jump to the character. I figured that it would be a simple thing like call a jump function when the player is at the apex of his jump to see if he double jumps. Yet again it was proven to me that I need to think a little differently to program video games. Just like in my last blog I was thrown off by the Y direction even though I try and remember that the Y Axis is positive in the down direction. The Y Velocity needs to be more negative to speed the character up in the up direction. Once I wrapped my head around this, I realized that the DoJump function is called in the ApplyPhysics function which is called every single Update. This not only meant that I could not simply call a function to force the character to jump but I had to take into account DoJump’s continuation of the jump. So people have to keep this in mind when writing their first XNA programs, most actions are handled by passing over the same function multiple times per second. Once I figured all of that out, I came to the conclusion that I would have to check for when the character is in the air in the DoJump function and reset the jump animation and launch him in the up direction.

// If we are in the ascent of the jump
if (0.0f < jumpTime && jumpTime <= MaxJumpTime) { // Fully override the vertical velocity with a power curve that gives players more control over the top of the jump

velocityY = JumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
}
else
{
// Reached the apex of the jump and has double jumps
//if velocityY <> 0 to double jump on descent
if (velocityY <> -MaxFallSpeed * 0.5f && !wasJumping && numJumps < MaxDoubleJumps) { jumpSound.Play(); velocityY = JumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower)); jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds; sprite.PlayAnimation(jumpAnimation, true); numJumps++; } Else { jumpTime = 0.0f; } }

Basically the if statement checks to see if the player is slowing down at the top of their jump (because gravity is pulling him down) then allow him to jump again if he has enough remaining double jumps. This ended up working very well. Also note that I overloaded the PlayAnimation method to allow you to reset the animation. Finally I check at the top of the DoJump function whether or not the player is on the ground, then I allow him to jump again.

if (isOnGround)
numJumps = 0;

With that you should now have a double jump. I set it to three jumps, so a player can tap jump 3 times. He will reach the apex of the jump and then play the jump animation again. When the player reaches 3 jumps he will fall to the ground. If you have any questions please post in the comments section.

2 comments:

  1. Hi..
    Nice tutorial you have there.
    Do you have a code sample for the XNA double jump and melee attack?
    Would really wanna appreciate it if you could show a sample.
    Thanks...

    ReplyDelete
  2. I don't have the code on this computer but I can tell you the easiest way to create a Melee attack on that original tutorial is to set an IsMeleeAttacking boolean and during that time play your animation until the attack time is over. If there is a collision with the player during that time, perform the logic to signify a hit.

    i.e. If(PlayerRectangle.intersect(EnemyRectangle) AND Melee attacking)
    Then Enemy.TakeDammage()

    ReplyDelete