Scratch Cookbook
上QQ阅读APP看书,第一时间看更新

Sprites interacting with other sprites

When making your story, there is a good chance you will want to get movement on the stage so your story is a bit more exciting. This recipe focuses on how to have sprites react when they are touching or moving around other sprites.

Getting ready

For this recipe, we're going to continue working with the story we built in our previous recipes of this chapter. Our goal for this recipe is to get Frog to move across the screen and react to Monkey Mike when they touch.

How to do it...

We're going to bring back some scripting from our first chapter to get the job done. Let's get started:

  1. To begin the movement of Frog, the user will have to click on the sprite. We'll need to provide directions to the user in order to accomplish this. At the end of Frog's dialogue (from before), add another say block and give it the text Click me to get me moving.
  2. Now we'll introduce a new top hat block. This is the when this sprite clicked block, located under the Events blocks category. It changes based on the sprite you are working on, so for ours it should indicate you are working with Frog.
  3. We'll drag this block over and begin a new script in the script area. We are going to get horizontal movement going on Frog, the same way we did in Chapter 1, Getting Started with Scratch.
  4. Drag over a forever loop from the Control blocks underneath our new top hat block.
  5. Place a move 10 steps block (from Motion) and the if on edge, bounce block (also from Motion) into the forever loop.

    Change the number of steps to something low, such as the number 2, to make sure the sprite moves slowly.

  6. Recall also from the previous chapter that we need to change the sprite settings for Frog so that the sprite remains oriented the way it is and does not flip upside down as he bounces on the edge of the screen. Select the second orientation option in the 3-button option area of the sprite settings, as shown in the following screenshot.

    First we have to click on the settings option:

    And then:

  7. Now, if we run our program and click on Frog on the stage, we'll see Frog move slowly back and forth across the stage. Our next step will be to have the sprites interact when they touch.
  8. We want Monkey Mike to react when Frog comes in front of him. In order to do this, we'll need to head over to the Sensing category of blocks. We'll also need a new Control block. Change the sprite we are working with to be Monkey Mike.
  9. Underneath the dialog of Monkey Mike, drag over the forever block. This block is going to be triggered by a new sensing block.
  10. Into the loop you created in step 9, drag an if () then block.
    Tip

    If you are working in Scratch 1.4, steps 9 and 10 can be combined by using the forever if block. This block was eliminated in the new version of Scratch.

  11. Drag over the touching block inside the look you just created, as shown in the following screenshot:
  12. Change the touching block by clicking on the black drop-down arrow and selecting Frog.
    Note

    You can use the touching block to sense when your sprite is touching any other sprite, or the mouse-pointer, or an edge. This will be very useful when we get into more advanced topics in later chapters.

  13. Now we need to make Monkey Mike say something when he is "touched" by Frog. Drag in the say block (with time element) and drop it within the forever block.
  14. Change the text to say Hey Frog, don't block my view.
  15. Now run your program to give it a test! Frog will pass in front of Monkey Mike and Monkey Mike will state his observation that Frog is in his way. This will continue as long as you let it go.

How it works...

We have several important ideas of programming going on in this recipe. We've already discussed the movement in Chapter 1, Getting Started with Scratch, so we will assume you are good to go with that.

As in our previous recipes, we have several scripts—all running at once. We learned about a new block, the when this sprite clicked block. This block is similar to our other top hat blocks in that it starts off a script. You can always use this block to start something when you want that particular sprite to be clicked. This only applies to the specific sprite you are programming though, so if you want to click on Frog and have something happen to Monkey Mike, you'll have to use a workaround (we'll talk about this in the next recipe).

Now let's take a look at the script for Monkey Mike. You should have something like the following screenshot:

Once Monkey Mike finishes his dialog at the start of the story, he waits for Frog to pass by his field of vision. This block is essentially a combination of two other Control blocks available to us, so saves us time in terms of coding. We also have, available for use, the forever loop, which we've used before. Secondly, there is the if block. Any block with an if embedded creates an if statement, which is triggered when the statement is true. Our combined block that we used makes Monkey Mike constantly look for that statement to be true, and then repeats the code inside until the story is manually stopped.

Tip

If you are extending the story and still want Monkey Mike to say something about Frog passing by, you'll have to create a separate script to handle the forever if loop. This method of creating the code works well when you want nothing else added. However, now you can't add more talking after the loop. Also note that if your Frog isn't large enough, or close enough to the monkey, part of this code will never need to run.

There's more...

Perhaps we don't want Monkey Mike to comment on Frog passing by for the entire story—after all, there is a good chance this will affect how your story flows. We also have the option of using another block combination as an alternative.

Here, we see that we can integrate the repeat loop and the if loop to create a loop that looks for the trigger a certain number of times—in our case 10. You can change this number if you want to something lower or something higher.

The logic of this section of code isn't too complicated. We see that the repeat loop continues what is inside it, 10 times. In other words, our code will check the condition we set (that is, whether or not we are touching Frog) 10 times, and then will stop checking and move on to whatever else we tack onto this script.

See also

  • To see more ways for sprites to interact, check out the next recipe, Basic broadcasting and receiving. This recipe will teach you a lot about basic techniques of background communication that we'll need to use in future projects.