Determining What Happens when Two Sprites Overlap

The last two Scratch looks code blocks, shown in Figure 10.11, specify what happens when all or part of a sprite is covered by another sprite.

Figure 10.11. With these code blocks, you can determine what happens when two sprites overlap.


In Scratch, each sprite that you add to an application is assigned to a layer. For example, suppose you create an application with multiple sprites. When you add the first sprite to the application, it is placed at the topmost layer. When you add the application’s second sprite, it gets added to the top layer, and the previous sprite gets moved back one layer. Each additional sprite starts off on the top layer and stays there until you either add another new sprite or until you click on one of the sprites that was previously added, which moves the selected sprite back to the topmost layer.

By default, the first sprite would be placed on the top layer. The second sprite added to the application would be placed on the second layer, and the third sprite would be placed on the third layer.

Understanding the layer on which a sprite has been placed is important because the sprite’s layer assignment determines whether it remains on top or is displayed underneath another sprite when they overlap one another. Sprites at higher levels remain on top of sprites at lower levels.

Note

To better understand the importance of levels, consider what happens when you place five pieces of paper on top of one another on a desk. The piece of paper sitting on top (at the top layer) is visible, and your view of the other pieces of paper is obstructed. Now, reach into the middle of the stack of paper, pull out a sheet, and place it on top of all the other pages. By altering the page’s layer position, you have now made it visible.


In addition to controlling what happens to sprites by adding them to applications in a specific order, controlling their layer position, you can use the code blocks shown in Figure 10.12 to programmatically control a sprite layer location. For example, using the first code block, you can move a sprite to the top layer, ensuring that it remains visible at all times on the stage, even when other sprites come into contact with it.

Figure 10.12. To play, you must ask questions that can be answered with yes/no-style answers.


As an example of how to work with both of these code blocks, revisit the Ball Chase game that was presented in Chapter 9, where both of these two code blocks were used to ensure that end of game messages were displayed on top of all other sprites. In addition, the application also used these blocks to ensure that the cat overlaps the ball when it catches it.

..................Content has been hidden....................

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