background check

How do you get the background to move in Snap? How do you get trees or rocks or buildings to move when your hero goes across the stage? The quick answer is, you don’t. The stage doesn’t move. So how do you have moving tress, rocks, or buildings?  You make them sprites.

Making something a sprite doesn’t move it though, you need a script.  You also need to do some things outside of the script to ensure your sprites do what you want.  First of all, make sure that your sprite can only face one direction.  There are three buttons at the top that control the direction(s) your sprite can face.  Make sure to click the bottom one.That arrow pointing to the right means that the sprite can only point one way, and that’s the way the costume points by default.

Next, make sure the Draggable box is unchecked.  That keeps the player from moving your sprite around.

Now you’re ready for your script.  The important principle here is that when your hero moves right, your background sprites scroll to the left.  If you have a “forever” level, they need to also disappear and reappear when they get to the edge.  Scrolling is perfect for a custom block because you’re going to have more than one background sprite and you can reuse the same code with different speeds for the different sprites.   Sprites that are intended to be closer to the viewer move faster (and are generally lower on the screen) than sprites farther away.

Here is code for a sprite scrolling left.  Note that like any other code, it’s only one possible way,  not the only way.This allows you to plug in two parameters, the speed and the height.