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 a parallax background (Must know)

In this recipe, we will implement a parallax effect for the game's background images. This effect causes the background to move at a slower rate than the foreground, which in return helps to give the illusion of depth within our game. More specifically this technique is achieved by placing multiple layers in front of each other and moving them at different speeds along the x axis.

How to do it...

  1. To do this, we will need to implement a new object called ScrollingBackground with the following code:
    function ScrollingBackground() { this.width = 0; this.height = 0; this.deltaScroll = 1; this.InitScrollingBackground = function(texture, x, y, z, width, height, deltaScroll) { this.InitDrawableObject(texture, ...

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