025 : Jump

Balthazar:

First define how much gravity there should be in this world:

script.js: Define a gravity variable.
function init_game() { ... // Global world parameters. _game.friction = 0.15; _game.gravity = 0.5; }
Balthazar:

We need to set the initial jump velocity and the max allowed velocity for the player

script.js: Define the initial jump velocity.
function init_player() { ... _player.velocity_x = 0; _player.velocity_x_delta = 0.8; _player.velocity_x_max = 3.5; _player.velocity_y = 0; _player.velocity_y_jump = -10; _player.velocity_y_max = 10; }
Balthazar:

We need to apply gravity to the player (just like we did with friction).

script.js: Update the player y-velocity to account for gravity.
function update_player() { ... // Apply the global world effects on the player. _player.velocity_x *= (1.0 - _game.friction); _player.velocity_y += _game.gravity; if (_player.velocity_y > _player.velocity_y_max) _player.velocity_y = _player.velocity_y_max; ... }
Balthazar:

and finally check for the jump key to initiate the jump. In this case, Up arrow, 'w' and spacebar:

script.js: Add support for the jump keys.
function check_input() { ... // Right arrow or 'd' to move right. if (_game.keymap[39] || _game.keymap[68]) { _player.velocity_x += _player.velocity_x_delta; if (_player.velocity_x > _player.velocity_x_max) { _player.velocity_x = _player.velocity_x_max; } } // Up arrow, 'w' and spacebar to jump. if (_game.keymap[38] || _game.keymap[87] || _game.keymap[32]) { _player.velocity_y = _player.velocity_y_jump; } }
Balthazar:

Go ahead and run your code now.

You:

The player falls off the bottom of the screen!

Balthazar:

That's because gravity is pulling the player down - that't what gravity does.

You:

But it was supposed to stop once it hit the platform. How are we going to fix it?

GOTO 029