Adjusting the timing
There is a good chance that the story you have in mind includes more than one sprite. This means that you are also going to need to think about timing in your story since you don't want to have both sprites talking at the same time.
Tip
Timing can become more and more complex depending on the program, especially as you add more and more sprites and length to your story. A good way to handle this situation is to think ahead of time as to when you want your sprite to speak, and when you want them to simply observe. Having this planned out will help you program much more quickly.
Getting ready
Continuing with our existing story, add another sprite. For example, we added a frog to our program in the bottom center. Copy over the same code (that we used for the first sprite) to make the second sprite think and talk. Note that if you duplicated the sprite, this is done for you. You'll notice both of them talking at the same time, which is what we are going to learn to fix (see the following screenshot showing roughly what you should see). Our example is relatively simple, but you can apply this concept to even the most complicated Scratch programs.
Tip
Remember, you don't need to recreate the code for the new sprite. With the original sprite selected, click and drag all of the code (beginning with the top hat block) on top of the picture icon of the sprite located below the stage. Your code should then appear in the script area of both sprites.
This is what you should see when you run your program:
How to do it...
Our next step is to get the timing right, and then we'll worry about making our characters say something different from each other. Follow these steps:
- For our story, we want Monkey Mike to start things off and have Frog follow. We know that Monkey Mike thinks for two seconds, so there needs to be a delay of at least two seconds on what Frog thinks. Notice that in the Control blocks we have a block called wait 1 secs with the 1 being an input box for you to change the number. While working in the script area for Frog, drag over the wait block and place it directly underneath the block with the green flag, as shown in the following screenshot:
- Next, change the value in the block you just dragged over to 2 instead of 1.
- Now, we'll want to add another wait block in between the think and say blocks we have for Frog. Make this block 2 seconds long as well.
- If you run your program now, you'll still notice a bit of a problem. We still need to make Monkey Mike wait while Frog is talking. Return to the script area for Monkey Mike. Place a wait block in between the think and say blocks, just as we did for Frog. Change this value to 2 as well.
- Now, change the text of Frog's thought bubble to I wonder if Monkey Mike notices me down here. Then, change the say block to Hi, you can just call me Frog.
- Run your program and see what happens. It should flow much better now that we've adjusted the timing.
How it works...
So now you're probably wondering what is happening behind the scenes. Let's look at the two pieces of code.
Our code for Monkey Mike is shown here:
And here is the code for Frog:
The biggest thing to notice in thinking about how the script runs in our program is that since both scripts begin with the block with the green flag, both scripts start simultaneously.
Note
In essence, you can have an unlimited number of scripts running simultaneously in Scratch. We'll learn soon how to get different scripts started with other Events blocks. This will give us much more control over our programs.
While we may have made it appear—when watching the story—that the sprites are waiting for each other, really what is happening is that each script is playing independently of the other. We just adjusted the timing effectively to give the story the timing we want.
There's more...
Maybe you don't want to have to click on the green flag to start the story. An alternative is to use the when key pressed top hat block instead, also from the Events category:
If you replace the green flag block with this new block, your story will start by pressing a specific key on the keyboard. By default, the block is triggered by the Space bar. You can change this by clicking on the black down arrow located next to the word space.
You'll see that this is the block we will use quite frequently (to get different aspects of our program to start) when we get into creating games and other programs.
For now, we'll stick with using the standard green-flag block for most script starting, but think of this as a good alternative in your own programming.
See also
- For more details on timing, see the Adding words to a sprite recipe
- To see how to get your sprites to interact, take a look at our next recipe, Sprites interacting with other sprites
- Later in this chapter in the Basic broadcasting and receiving recipe, we'll explore a fun way to get background communication in your program