Changing Sprite Coordinates

Scratch provides four motion blocks that modify the location of a sprite on the stage either by assigning it new coordinates or by changing the sprite’s coordinates by incrementing or decrementing their values. These code blocks are shown in Figure 5.8.

Figure 5.8. These code blocks provide the ability to modify a sprite’s location by changing its coordinates.


The following script demonstrates how to move a sprite across the stage in a series of eight steps. When first started, the script moves the sprite to the left-hand side of the stage, and then, using a loop, the sprite is moved by incrementing the value assigned to the X-axis coordinate by 50 and its Y-axis coordinate by 10 each time the loop repeats itself. As a result, the sprite is repeatedly repositioned and thus moved across the stage (in a descending angle over a period of eight seconds).

Bouncing Sprites Around the Stage

As a sprite is moved around the stage, it may eventually come into contact with one of the edges of the stage. Using the motion block shown next, you can instruct Scratch to bounce the sprite off of the edge of the stage.

The following script demonstrates how to use this code block to bounce a sprite around the stage:

This script reverses the direction that a sprite is traveling whenever it collides with the edge of the stage. If you were to add this script to the cat sprite in a new application, the cat would move across the stage from side to side until you halted the application’s execution.

..................Content has been hidden....................

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