arrow_back Animate the Narrator (Part 2 of 2)
To make code easier to read, computer scientists use something called “functions.” These are reusable blocks of code that do one task. In Scratch, you can also define your own function.
For this add-on, you will create a function that makes the narrator “say” things while the mouth moves. To create your own function block, click on “more blocks,” then click “make a block.” This block will make the narrator talk, so name it something that makes sense, like “talk.” A large purple block with an arc at the top that says “define talking” will appear in your scripts area, and a block called “talk” will appear among the blocks you can choose from.
Click on the “talk” block. Right now, it does nothing. That’s because “talk” hasn’t been defined. The first time you see a new word, you probably look up its definition in a dictionary. The “define” block works just like a dictionary. It is where the program goes to look up the meaning of “talking,” and find out what it should instruct the computer to do. Place the code that makes the narrator talk under the “define” block. Then, click on the “talk” block again.
Now the narrator’s mouth looks like it’s moving while he talks, just like it did when you ran the code without the function. But, wait a minute. The point of a function is that you can reuse it. Right now, this function only makes the narrator say one thing: “This main character’s name is Dino.” In order to use the function you created in more than one spot in the program, you need to make it tell the narrator to say the different words and phrases you created in Part I. Luckily, functions can receive input, which is kind of like special instructions you give the function to make it do what you want.
Input will allow this function to say whatever gets sent to it, rather than just repeating “this main character’s name is Dino.” To give the function input, right click on the block definition, then click “edit.” An edit block menu appears. Click on “options” in the bottom-left corner. Doing this opens a menu of possible inputs you can add to your new block. In code, words and sentences are called “strings.” So, click “add string input.” This adds a box next to the name of the function up top. It starts off as “string1,” but that isn’t a very descriptive name. Try naming the input something that describes how it’s used, like “narrator’s words,” then click “OK.” The “talk” block now has a space next to it, which is where you’ll put the words you want the narrator to say. You can either cut and paste or retype the narrator’s dialogue into the “talk” block.
Now, you’re sending “this main character’s name is Dino” as input to the “talking” function. Next, the program needs to know how to use the input. Click on “narrator’s words” in the definition, and drag it into the first “say” block. Click on the “talk” block to see this work. The narrator says, “This main character’s name is Dino” while its mouth moves.
Now, drag out another “talk” block. This time, write the next line of the narrator’s dialogue for your story. In this example, the next line is, “He is friendly and likes to dance.” Click on the two “talk” blocks.
The narrator says, “This main character’s name is Dino,” then, “He is friendly and likes to dance.” Great. You can send any string input to the talking function; the narrator will say whatever you input while moving its mouth.
Finally, replace all the “say for __ seconds” blocks in your code with “talk” blocks.
Now, it’s your turn: Create a new block by going to the “more blocks” menu, clicking make a block, and naming it something that makes sense.
Place the block stack you made in the last screencast under the “define” block.
Edit the block to take string input. Edit the “say” block to “say” the input. Use the new block instead of the “say for __ seconds” block in your story.