3. Places to Go
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 new location, 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 chance to 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 backdrop switches 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 automatically organize your code. This code reads, “when backdrop switches to town, show” and “when I receive hide entrances, hide.” But, this sprite should do the opposite. It should hide when the other sprites show, and it should show when the other sprites hide. Switch the “show” and “hide” blocks.
After you make that change, the code reads, “if touching character, switch backdrop to house.” If the exit sprite touches the player sprite, the backdrop should switch back to “town.” Click the dropdown, and change this value to “town.”
When the backdrop switches to “town,” the entrance sprites shouldn’t hide, so remove 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, the backdrop 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 different exit costume for each of the backdrops. Add code to switch the exit sprite’s costume to 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, and change 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 costume appears. Great! Computer scientists often reuse previously written 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 it to 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 backdrop switches” and “switch costume” blocks.