Parallax background scrolling

We will create a background movement that moves in a different speed than the runway. This is also known as parallax scrolling, and creates an illusion of depth.

Prepare for lift off

Make sure the following image is ready in the project inside the images folder:

Prepare for lift off

Engage thrusters

Let's adjust the movement of the background with the following steps:

  1. Inside the runway.js file, we append the following code before the end of the tick function:
    game.view.floor.style.backgroundPositionY = round * game.BACKGROUND_MOVEMENT_SPEED + 'px';
  2. This moving speed is something that we may fine-tune later. We will put such a variable in the setting.js file for easier access:
    game.BACKGROUND_MOVEMENT_SPEED = 5;

When we test the game in the browser, the background moves slower than the runway. This creates a perception of depth. The background appears to be far away from us.

Objective complete – mini debriefing

We have completed the task by adding a background that looks like it's far away from the player and runway.

Parallel scrolling background

I guess you have had this experience already. Imagine you are inside a car and you are looking outside. Things nearer to you will pass by the car very fast; things that are far away seem to move slower. Things that are very far away, like the mountain in the background, do not seem to move at all:

Parallel scrolling background

This is because of how we see things in the perspective view. In the digital world, we call this parallax scrolling.

Therefore, when we are moving game objects around, we can make use of the speed differences to create an illusion of depth.

In our game, the background is something far away in space. The movement of the background should move much slower than the player's running speed.

The normal speed is 50. We can set the background movement speed to be 1/10 of the normal speed:

Parallel scrolling background
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.201.187