Please note: This theme has not been updated to reflect Scratch 3.0. Visit this help article for more info.
Transcript

In this video, you'll add more dialogue to your story.

To do this, you'll create a function by making your own block.

This'll make it easier to create dialogue.

It takes three blocks to make Malik talk, broadcast, say, and switch costume.

To create a long conversation, you would have to copy these blocks over and over.

That will make the code long, difficult to read, and hard to edit.

A function makes the same thing happen, but with a lot less code that is easier to read and work with.

Functions allow programmers to reuse code easily.

In this project, you will make it easier to reuse the code containing the broadcast, say, and switch costume blocks, by combining these blocks into a function.

Make a function and call it speak.

To make the character say different things, add a string parameter.

Give the parameter a descriptive name, like sentence.

Drag the code from under the one-flag clicked block to under the define speak block.

Click on the speak block in the more block section.

The mouth moves and the sprite says the words in the say block just like it would if you click the block stack.

Place the new speak block under the one-flag clicked event.

When you click the flag, the code you built in the last video runs.

The speak block has an input.

Type in what you want Malik to say in that box.

This example says, what do you want for lunch?

The define speak block has an oval labeled sentence.

Sentence is a parameter that gets set to whatever gets passed in, which in this case is, what do you want for lunch?

Malik will say the words in this sentence variable.

Replace the text in the say block with sentence.

To do this, drag sentence from the define block into the say block where the words Malik says used to be.

Test the code by clicking the flag.

Great!

Test again using another speak block.

This time input a different sentence.

This examples uses, I don't like peanut butter and jelly sandwiches.

Try it out again.

Awesome!

The speak function now takes an input so the sprite can say different lines instead of repeating the same phrase over and over.

Now it's your turn.

Make a function called speak with a string input called sentence to make Malik speak.

Replace the words in the say block with the sentence input variable.

Use another speak block to write a new line for Malik to say.

Instructions

  1. Make a function called "speak" with a string input called "sentence" to make Malik speak.
  2. Replace the words in the "say" block with the "sentence" input variable.
  3. Use another "speak" block to write a new line for Malik to say.
Attributions
  • Emotion Animation If there weren't functions! by EmmyNoether (https://scratch.mit.edu/projects/46267522/#editor licensed under CC BY-SA 2.0 (https://creativecommons.org/licenses/by-sa/2.0/)