Time for action – overriding the Update() method – part 2
Inside the Public Methods region of the Player class, add the
Jump()
method:public void Jump() { velocity.Y = -500; }
Add the following to the
Update()
method right after theif
statement for detecting movement to theright
, and before the check for which animation to play:if (keyState.IsKeyDown(Keys.Space) || (gamePad.Buttons.A == ButtonState.Pressed)) { if (onGround) { Jump(); newAnimation = "jump"; } } if (currentAnimation == "jump") newAnimation = "jump";
Still in the
Update()
method, add the following line right before the call tobase.Update()
(outside of the conditional for the player being dead—being dead will not prevent the player's body from falling due to gravity):velocity += fallSpeed;
Execute the Gemstone Hunter project. Use the Space bar or the A button on the gamepad to jump.
What just happened?
When you execute the game, the player will fall with increasing speed and land on the...