Parallax background
You want mountains in the background that move slower than the ground when the camera moves. This effect is called parallax.
In the editor
Section titled “In the editor”- Import the image of the mountains as a sprite.
- In the Camadas panel, create a Fundo (background) layer called
Montanhasand drag it below the others (only the sky stays behind it). - In the layer’s Inspector, choose the Sprite and check Repetir
H, so the image repeats horizontally.
In the code
Section titled “In the code”In an object that exists in the level (a controller with no sprite will do):
void Fase::onUpdate() { setLayerPosition("Montanhas", scene().camera.x * 0.5f, 40.0f);}How it works
Section titled “How it works”scene().camera.xis the position of the camera: the left edge of what shows on the screen.setLayerPosition("Montanhas", x, y)puts the corner of the layer’s image at(x, y)of the room.- Multiplying by
0.5fmakes the layer move half of what the camera moves. On the screen, it seems to pass slower than the ground, like something far away.
The number is how much the layer “sticks” to the screen: 0 leaves the image fixed to the level (no parallax), and 1 leaves it fixed to the screen (it never moves). The closer to 1, the further away it looks.
Variations
Section titled “Variations”Several layers. Use one number for each: the further ones, closer to 1.
Clouds that move by themselves. No code needed: in the Inspector of the Fundo layer, set a Velocidade (speed) in X, like -0.2.
