Adding parallax scrolling to the mountains in the background

Parallax scrolling is a special scrolling technique that creates the illusion of depth in a 2D scene. Therefore, the objects in the background move slower than the objects in the foreground when the camera moves by.

We will now implement a parallax scrolling effect for the mountains in the background of the game screen.

Add the following import line to the Mountains class:

import com.badlogic.gdx.math.Vector2;

After that, add the following lines of code to the same class:

public void updateScrollPosition (Vector2 camPosition) {
  position.set(camPosition.x, position.y);
}

Next, make the following changes to the same class:

private void drawMountain (SpriteBatch batch, float offsetX, floatoffsetY, ...

Get Learning Libgdx Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.