Time for action - give me an "M"

In the next act, we introduce Mom. The letters M, O, and M are currently hidden, so let's bring them out:

  1. We're going to start with Second M. Select Second M from the sprites list so that we see the scripts area.
  2. We will control the Second M sprite with the when I receive hi mom control block. Drag that block into the scripts area.
  3. To display the letter M, we need to add the show block.
  4. Click the flag to see the script run so far. As Happy Birthday beats on the screen, the second letter M appears on the screen.
  5. Click the stop button when you're ready to continue.
  6. From the Control palette, snap the repeat 10 block to the bottom of the show block.
  7. We're going to make the M a mosaic. From the Looks palette, add the change color effect by 25 block to the repeat loop. Change the graphical effect from color to mosaic by selecting mosaic from the drop-down list.
    Time for action - give me an "M"
  8. Click the flag and watch the effect of our action. Our M is a mosaic, but it's no longer readable.
  9. Click Stop when you're ready to continue.
  10. From the Looks palette, double-click on the clear graphic effects block to redisplay the letter M.
  11. Snap another repeat 10 block to the bottom of the first repeat 10 block.
  12. Insert a second change mosaic effect by 25 block into the new repeat 10 block. Change the effect from 25 to -25.
  13. Now we need the script to announce when it's finished. Add a broadcast block and create a new message next m.
  14. Let's see what we've done. Click the flag.

The following screenshot shows the letter M as we apply the mosaic effect by 25:

Time for action - give me an "M"

The following screenshot shows the finished product after we apply the change mosaic effect by -25:

Time for action - give me an "M"

What just happened?

Finally, we acknowledged the Happy Birthday sprite's "hi mom" broadcast message. "Hi mom" was the cue that told Second M to enter the stage with dramatic effect.

Once the letter M is displayed, it splits apart to form a pattern of little Ms before it reconstituted itself into a whole letter. The code that created our pattern was straight forward. First, we applied a mosaic pattern with an effect of -25 each time the repeat block ran. The effect repeated 10 times. The second repeat block also ran 10 times, but it changed the mosaic effect by -25 each time. The second repeat block undid the effects of the first repeat block.

Loop with repeat

Like the forever block, the repeat block creates a loop that runs the blocks inside the loop. The repeat block differs from the forever block in that we specify a number of times to run the loop.

In addition to entering a whole number in the repeat block, we can insert variables that represent numbers. We will review variables in Chapter 6.

Pop quiz

  1. If you want to loop through a set of blocks for a specific number of times, which block do you use?
    • Forever
    • Repeat
    • Broadcast
  2. Which of the following graphical effects splits a sprite into a pattern of smaller images of itself?
    • Whirl
    • Pixelate
    • Mosaic
  3. How do you copy a script from one sprite to another?
    • Right-click on the script, select duplicate, and drag the script to the second sprite
    • Highlight the script, press Ctrl + C to copy it, and press Ctrl + V to paste it on the second sprite
    • You can't copy scripts from one sprite to another

Incremental improvements

Programming in Scratch, like most creative endeavors, is an iterative process. That means we build our scripts one block at a time, and we can expect to make multiple versions until we get the results we want.

Our initial efforts may have a bug, which means our script doesn't perform as we expect it to. Bugs happen for any number of reasons and as we encounter a problem, we troubleshoot it. Throughout the book, we'll have many opportunities to troubleshoot our scripts, but let's focus mainly on efficiency.

Remember how, as we stepped through the exercise, we ran the clear graphic effect block to reset our sprite to its default state? In our code, we essentially cleared the graphic effect with the second repeat block because it did the opposite of what the first repeat block did.

If after we change the mosaic effect by 25 for 10 consecutive times, we add the clear graphic effect block, we create a simpler script. We get the same results, and we have less points of failure to troubleshoot in the future.

The following screenshot shows the revised script for the Second M sprite:

Incremental improvements
..................Content has been hidden....................

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