O'Reilly logo

Instant HTML5 2D Platformer by Aidan Temple

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing physics (Must know)

In this recipe, we will look at implementing a jumping behavior for the player. In order to implement this feature, we will need to adjust that section of the game framework, which is responsible for detecting and updating user input. By doing this, we can determine when the player has pressed either the Space bar key, the W key, or the up arrow key in order to make the player jump.

How to do it...

  1. In order to implement jumping behavior, we will need to implement additional behaviors within the Player object. Open this object and declare the following variables at the top of the object:
    this.maxJump = 64; this.jumpTime = 1; this.jumpVelocity = ((Math.PI / 2) / this.jumpTime); this.position = 0; this.terminalVelocity ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required