In this video, you will customize the object that passes by the sprite, and you'll create a variable you'll need for the parallax scrolling effect.
To start, select the Objects sprite.
Add a new when flag clicked event.
Then under it, add a switch costume and set Y blocks.
Click the Costumes tab and check out the different costume options.
Switch the costume to whatever you like.
Click the flag and press the right arrow key to test.
That looks good.
Tinker with the value in the set Y block to change the object's height.
Next, set the distance of the object from the walking sprite.
Create a distance variable.
Name the variable distance and select For this sprite only.
This step is important.
When a variable is for all sprites, its value is shared between sprites or between its clones, so when the variable is set in one sprite or clone, it changes the same variable in all the others.
When you choose for this sprite only, you're making a local variable.
Local is the alternative to global.
It means that only one sprite can use or change that variable's value.
Global variables, however, can be accessed or changed by any sprite.
With local and global variables, it's not that one is better than the other.
You just use them in different situations.
Add a set variable block under the set Y block, and make sure the distance variable is selected.
Choose a distance between one and 10.
Next, use the distance variable to set the size and speed of the background object.
Add a set size block.
The closer an object is, the larger it will appear.
The farther away an object, the smaller it will appear.
So distance and size are inversely proportional.
As one goes up, the other one goes down.
Add a division block inside the set size block.
In the left blank, type 200.
In the right blank, add the distance variable.
If the distance is two, then the size of the object will be 100%.
If the distance is greater, meaning that the object is farther away, then the size will be smaller.
For example, when the distance is five, the size would only be 40%.
Click the flag to test.
Tinker with the value in the distance variable.
A big value in the distance variable makes the object look really small.
Next, program the objects to move faster or slower depending on their distance.
Speed and distance also have an inverse relationship.
The greater the distance between oneself and an object, the slower it appears to move by.
The current speed of the object is constant.
When you press the arrow keys, the object sprite moves at a rate of 20 steps.
To make the objects move at a speed based on their distance, add a division block into the move blocks under both key pressed conditionals.
In the first value of the first division block, put a negative number.
This example uses negative 20.
Divide that number by the distance variable.
In the first value of the second division block, put a positive number and divide that number by the distance variable.
This example uses positive 20.
Click the flag to test.
Then continue to tinker with the value in the distance variable to see how this changes the size and speed of the sprite.
Add detailed instructions about how to make your project work in the Project Page.
Now it's your turn.
Set the costume and the height.
Create a distance variable and set the distance.
Set the size and speed of the sprite to be inversely proportional to the distance.
- Set the costume and height.
- Create a distance variable, and set the distance.
- Set the size and speed of the sprite to be inversely proportional to the distance.