Saturday, August 25, 2018

How do I implement parallax scrolling in 2D?


Suppose I have three layers: a foreground, an intermediate-ground and a background. What is involved in using these three layers to create parallax scrolling?



Answer



Divide the position of each layer by the “distance” you want it to have from the camera.


For example: Create a couple of variables to represent the camera position, cameraX and cameraY. Set these to equal the position of your character, possibly adding on a bit extra in the direction of movement.


The main layer would just be positioned at


mainLayer.x = -cameraX;


the middle layer at something like


middleLayer.x = -cameraX * 0.5;

and the far layer at


farLayer.x = -cameraX * 0.2;

Change the constants as needed.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...