Changing Sprite Costumes and Backgrounds

Depending on whether you have selected a sprite’s thumbnail or the stage thumbnail in the sprite list, several different code blocks are displayed when you look at Scratch’s looks blocks in the blocks palette. For starters, the first three code blocks are different, as shown in Figure 10.1.

Figure 10.1. The code blocks on the left are displayed when you are working with a sprite, and the code blocks on the right are displayed when you are working with the stage.


Both sets of code blocks have similar tasks, with one set focusing on working with sprite costumes while the other set is focused on working with the stage’s background.

Changing Sprite Costumes

Every sprite that is added to a Scratch application is capable of changing its appearance by changing its costume. Sprites can be assigned any number of costumes and switch between them at any time. To add a costume to a sprite, all you have to do is select the sprite’s thumbnail, click on the Costumes tab located at the top of the scripts area, and then click on the Import button. This opens a window that allows you to locate and select a graphic file to be used as a new costume for the sprite.

Every costume that is added to a sprite is automatically assigned a number and a name (based on the graphic’s filename). The first costume in the costume list represents the sprite when the application is started. However, using drag and drop, you can rearrange the order in which costumes are listed. In addition, using the first looks block shown in Figure 10.1, you can programmatically replace a sprite’s current costume by specifying the name of a different costume. For example, the following script demonstrates how to use this code block in a loop to repeatedly change a sprite’s costume 10 times at half-second intervals. The result is the generation of animation that makes it look like the bat is flying.

To change the costume of the sprite to which this script is added, select the costume’s name from the looks block’s drop-down list. The block’s drop-down list is automatically populated with a list of all of the costumes that have been added to the sprite. The costumes listed in the previous example refer to two costumes representing different views of a bat, as shown in Figure 10.2, and are supplied as part of a collection of graphic files that ships with Scratch.

Figure 10.2. Bat costumes.


Costume numbers are automatically assigned by Scratch as you import new costumes into a sprite. The first costume assigned to a sprite is given a costume number of 1. Each successive costume is assigned a higher number, as demonstrated in Figure 10.3.

Figure 10.3. Three costumes have been added to a sprite, each of which depicts a slightly different version of a blue dog. These costumes are numbered 1, 2, and 3 and are named dog2-a, dog2-b, and dog2-c, respectively.


Using the second looks block shown on the left-hand side of Figure 10.1, you can change a sprite’s costume to the next costume in the costume list. For example, the following script automatically changes a sprite’s costume whenever the sprite is clicked.

When executed, the script changes the sprite’s costume to the next costume in the list. By clicking on the sprite repeatedly, you continue changing the sprite’s costume. Once the last costume in the costume list has been displayed, Scratch will go back to the top of the costume list and start over, as depicted in Figure 10.4.

Figure 10.4. Scratch loops back to the beginning of the sprite’s costume list as necessary to fulfill additional costume switches.


The last looks block shown at bottom left of Figure 10.1 can be used to display a monitor that presents a sprite costume number on the stage. Alternatively, you can use this code block as input to any code block that accepts numeric input.

Changing a Stage’s Background Costumes

The looks code blocks on the right-hand side of Figure 10.1 are used to change the stage’s background and work identically to their counterparts that deal with costumes. For example, the following script demonstrates how to randomly set the stage’s background to one of three options.

Note that in addition to changing the stage’s background twice, this example also plays one of three audio files, depending on which of the three backgrounds is randomly selected.

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

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