Accessing Variables Belonging to Other Sprites

Although data stored in local variables can only be changed by scripts belonging to the sprite to which the variables have been assigned, Scratch does allow scripts belonging to other sprites to view data stored in variables belonging to other sprites. To view data stored in another sprite’s local variables, you need to use the sensing block shown in Figure 7.9.

Figure 7.9. Using this code block, you can create a script that can view data stored in another sprite’s local variables.


This code block lets one sprite retrieve another sprite’s X position, Y position, direction, costume number, size, and volume. It also lets you retrieve values assigned to another sprite’s variable. As demonstrated in Figure 7.10, you can click on the code block’s right-hand pull-down menu, and it will display a listing made up of the stage and all of the sprites in the Scratch application.

Figure 7.10. Specifying the name of the sprite whose variable you want to access.


After selecting the stage or a sprite, you can use the drop-down menu located on the left-hand side of the code block to select and retrieve information for any of the specified items that are listed. A gray horizontal divider bar located at the bottom of the resulting list denotes the sprite’s list of variables, separating the list from other available data, as demonstrated in Figure 7.11.

Figure 7.11. Selecting the variable whose data you want to access.


Using this code block, you can retrieve data stored in any sprite’s local variables. However, all you can do is read the value assigned to those variables; you cannot modify them. The only variables that can be remotely modified are global variables.

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

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