In this video, you’ll program the “exit” sprite to show, hide, and change the backdrop.
This sprite will work similarly to the entrance sprites. When the player sprite enters a newlocation, the exit sprite will “show.” If the player sprite touches the exit sprite,the backdrop will change back to “town,” and the exit sprite will hide is is a chanceto reuse code you’ve already written to approach a new problem.
To start, click the exit sprite. Currently, there isn’t any code for this sprite.
Click the house-entrance sprite, and drag the “when I receive,” “when backdropswitches to,” and “when flag clicked” block stacks into the exit sprite.
Click the exit sprite, then right click the scripts area and click “clean up” to automaticallyorganize your code. This code reads, “when backdrop switchesto town, show” and “when I receive hide entrances, hide.” But, this sprite shoulddo the opposite. It should hide when the other sprites show, and it should show when theother sprites hide. Switch the “show” and “hide” blocks.
After you make that change, the code reads, “if touching character, switch backdropto house.” If the exit sprite touches the player sprite, the backdrop should switchback to “town.” Click the dropdown, and change this value to “town.”
When the backdrop switches to “town,” the entrance sprites shouldn’t hide, soremove the broadcast “hide entrances” block.
Try it out all together. Click the flag, and move the character into the house. The exit,which looks like a door, appears! Next, move the sprite to the exit and… awesome, thebackdrop changes, and the exit sprite hides. Nice job!
Next, move the character to the cave. The exit appears, but it still looks like a door.
Click the “exit” sprite, then click the “costumes” tab. There’s a differentexit costume for each of the backdrops. Add code to switch the exit sprite’s costumeto the one that matches the current backdrop. Return to the “scripts” tab, and add a“when backdrop switches to” block from the “events” menu. Select “house”from the dropdown. Then, open the “looks” menu, add a “switch costume” block, andchange the dropdown to “house-exit.”
Duplicate this stack for the other two locations. When the backdrop switches to “forest,”the “forest-exit” costume should appear. When the backdrop switches to “cave,”the “cave-exit” costume should appear.
Click the flag, and go to each of the three locations to test that the correct exit costumeappears. Great! Computer scientists often reuse previouslywritten code, and they frequently change that code to fit the needs of their current program.
In this video, you reused the code from the location sprites, but you had to change itto accomplish a different, but similar task. Now, it’s your turn:Copy the code from the house sprite to the exit sprite.
Exchange the “show” and ”hide” blocks. Change the “switch backdrop” value to“town.” Remove the “broadcast” block.
Make the exit sprite change costumes depending on the backdrop using the “when backdropswitches” and “switch costume” blocks.