Most of your rooms will have some sort of background, whether it’s a solid color, a moving background, or a more complex parallax effect.
Splash screens.
Backgrounds for levels.
Moving backgrounds for infinite runner type games.
Backgrounds are loaded in the same way as sprites. Load in the space background from the resources and name it bg_1.
If you wanted to move the background to the left, you can set the Horizontal Speed to a negative value, for example -2, in the room settings, as shown back in Figure 12-3.
Setting Up a Background
If you test it, you can move the background around.
Parallax Effect
Multiple backgrounds can be combined to create a Parallax Effect. This is when the foreground moves more quickly that the backgrounds.
Start a new project, and load in sprites from the asset folder Mountain Backgrounds.
The mountain in the preceding image will be set to move slightly faster than the rear layer, helping to increase the parallax effect.
Next, name and order them as shown in Figure 13-5.
Now assign the sprites bg_1 to Background_1, and similarly for bg_2 and bg_3.
Set up in a similar way with background_2 (with Horizontal Speed of -2) and background_3 (with Horizontal Speed of -3).
Test to see your parallax effect.
which would set Background_0 x position to 25.
Basic Projects
- A)
Set a system that changes background image upon pressing 1, 2, or 3.
- B)
Set up a tiled background that moves diagonally.
Advanced Project
- C)
Make the background move in the direction of the arrow keys when pressed.
- D)
Make a parallax with three layers, use code rather than room settings to make them move.
Useful Functions
which would set it to visible, false would hide this layer.
Summary
You now understand how to add backgrounds, make them move, and combine them to create a parallax effect.