Using parallax scrolling to simulate depth

To increase the sense of movement and depth, we are going to use a technique called parallax scrolling. This means that objects will move at different speeds based on their distance from the viewer. In real life, you can see this phenomenon when you are riding in a train or a car. When looking out of the side window, you will notice that objects that are close to the vehicle seem to be moving past a lot faster than objects that are further in the distance. Objects on the horizon hardly seem to move at all.

This relative movement is what we perceive as depth or distance. It allows us to guess the three-dimensional proportions of a space. We are going to simulate that 3D effect with our 2D sprites to make the scene a bit more realistic and engaging.

Prepare for lift off

First we need some more sprites to use as objects moving past the viewer. Let's create some rocks and seaweed that we can place along the floor of the scene.

  1. Choose a new sprite from the Scratch library. Let's use the Rocks sprite.
  2. We need a few different costumes for the sprite. Go to the Costumes tab, right-click on the costume, and choose duplicate.
  3. Click on the flip left-right button to mirror the second costume.
  4. Make two more duplicates of these costumes. Scale both down a bit with the Select tool.
  5. Make sure to set the center point at the bottom middle of each costume with the Set costume center tool. This will make sure we have better control over the height position of each sprite.
  6. There is no library image for seaweed, so we will have to draw those ourselves.
  7. Click on paint new costume to create a new empty costume canvas.
  8. In Vector Mode, we draw a circular outline using the Ellipse tool.
  9. We fill the ellipse with a lighter shade of green.
    Prepare for lift off
  10. Then, we switch to the Reshape tool as shown in the preceding screenshot. Some draggable circles show up along the edge of the shape.
  11. Let's move the circles around to twist and stretch the shape until it resembles seaweed. We can also add more circles by dragging the edge where there is no circle yet. Dragging the outline will cause a new circle to be created. Double-clicking on a circle will remove it.

The result should look something like the following screenshot:

Prepare for lift off

Once we are happy with our first seaweed drawing, we will create another with a slightly different shape and color.

Six different costumes should be enough to create a nicely varied background as shown in the following screenshot:

Prepare for lift off

Engage thrusters

Let's move on to the scripting. We will use the cloning method again to create multiple objects from a base sprite shown as follows:

  1. As usual we start with a when <green flag> clicked block.
  2. First, we click on hide to hide the original sprite from view.
  3. Then, we make it go to front to place it on top of everything else using the go to front block.
  4. Next, we make it go back five layers by entering 5 in the go back () layers block. This is to make sure that the sprite will be at the depth where we want it to be. If the sprites are not layered properly, the parallax effect will look weird.
  5. We position the sprite at its starting place with a go to x: () y:() command; enter 240 and -150 respectively.
  6. This sprite also needs its personal speedMultiplier variable, just like the background images.
  7. After creating the variable, we set its value to 2.
  8. After this, we will start the forever loop, which will spawn the clones.
  9. We add the wait () secs block.
  10. For the empty slot, we pick the random numbers 1 and 5. These values give good results.
  11. We also change the height to a random number between -170 and -130.
  12. Also, we switch to a random costume, numbered 1 to 6.
  13. After all these randomized choices are made, we can create our clone by using the create a clone of <myself> block.
    Engage thrusters

That concludes the base sprite script. Next we will create a clone script. This will control how the clones will move, just like in the previous examples.

  1. We first need to define a variable called position only for this sprite. This variable will store a virtual x position value. We use this method to allow a value to go beyond the borders of the stage.
  2. We will start a new script by using the when I start as a clone block.
  3. We will set the position to 300 in the set <position> to() block. This is beyond the right border of the stage.
  4. Then, enable the show block to show the cloned sprite.
  5. We will move the clone inside a repeat until () loop.
  6. We first change the position variable by scrollingSpeed * speedMultiplier.
  7. Then, we set x to position using the set x to () block.
  8. To fill the condition slot, we check when the position is less than -300 by entering -300 in the position < () block.
  9. When that happens, we can delete this clone by using the delete this clone block as it is beyond the left edge of the stage.
    Engage thrusters

Our first scrolling sprite is now finished. Test the script and we will see that the rocks and seaweed move a lot faster than the background image.

To make full use of the effect, we need a few more layers of objects though. We will create a few more background objects moving at different speeds:

  1. Let's copy the Rocks sprite twice.
    Engage thrusters

    For the first copy, we edit the script in a few places.

  2. We make the sprite go back 10 layers instead of 5 by entering 10 in the go back () layers block.
  3. We enter 80 in the set size to () % block to make the sprites a bit smaller. These objects are a bit further in the distance.
  4. We will emphasize by placing them a bit higher on the stage. We change its starting y value to -100.
  5. These objects will also move a bit slower because they are more distant, so we change the value of speedMultiplier to 1.5.
  6. We also need to change the random height range to correspond with the new basic height. A value between -90 and -110 should be okay.

For the second copy, we make similar changes with the following values:

  • Enter 15 in the go back () layers block
  • Enter 60 in the set size to () block
  • Change the starting value of y to 70
  • Change the value of speedMultiplier to 1
  • Enter the values -60 and -80 for the height range

The parallax background is now ready to go as shown in the following screenshot:

Engage thrusters

Objective complete – mini debriefing

We added a lot of graphical spectacle in this project, but after the initial effort it took to create the sprites, the scripting is fairly simple. All sprites behave the same. They just use different values based on where they are on the screen. You could even add more layers if you wish. Just keep in mind that all those clones require the computer's attention. When there are too many, you will notice that the game starts slowing down and skipping frames.

Something you might want to try is adding to or changing the costumes. The intensity of the effect depends a lot on the kind of images used. More horizontal images can increase the perception of depth in the scene while vertical objects in the foreground will increase the sense of speed.

Classified intel

A nice add-on is to create large silhouette shapes in the foreground. These objects should use a script similar to the scrolling background images. Like those background images, they will also need two identical sprites to scroll seamlessly.

First, we need to create the image. This will be a hand-drawn image as big as the stage. Let's draw a rocky floor in the Bitmap mode. The floor can contain a few outcrops and spires that obscure a part of the stage. This makes the complete scene more interesting. But we need to be careful about not obscuring too much of the important stuff like the diver and the starfish.

After creating the foreground images, we can drag-copy the script from the background to the new sprite.

Classified intel

We make a few additions to this script that we take from the parallax scrolling script:

  1. The images should be in the front layer.
  2. The speedMultiplier value should be quite high, somewhere between 2.5 and 4 works well; enter 4.
  3. Check whether the starting x positions for both foreground images are correct; enter -240 and 240 respectively.
  4. It's also possible to add multiple costumes that are chosen randomly on each loop. We can add a switch costume to () block inside the if () statement, after the x position in the x position < () block is reset. This functionality is taken from the parallax scrolling objects.

The following screenshot shows the final script:

Classified intel
..................Content has been hidden....................

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