arrow_back Passing By
In this add-on, you will program the clone of a particular costume to play a sound or say something when it passes the "objects" sprite.
To start, program the clone to check which costume is displayed.
This example uses the peacock costume, but you can use any costume in your project.
Add a "when I start as clone" block.
In this example, if the peacock costume is displayed, then the clone plays a sound and says something.
Add an" if then" block.
Next, check if the costume number equals the costume you want to perform the action using the "equals" and "costume number" blocks.
Program an action that the clone will perform if the correct costume number is displayed.
Add a "say" block or a "play sound" block.
You can add both types of blocks or just one.
Type in a message in the "say" block, or add a sound from the Scratch library.
select a “forever” block, and place it around your block stack.
Click the flag and the right arrow key to test.
The peacock plays the goose sound and says, "What's up?"
But, it keeps repeating this action for as long as it's on the screen, which is annoying.
To fix this, program the clone to wait until the dog has passed it.
Add a "wait until" block and a “less than” block.
In the left side, add an “x position” block from the motion menu.
In the right side, add an "x position" block from the sensing menu, and make sure the walking sprite's name is selected.
Now the code reads, “if the clone's x position is less than the walking sprite's x position, meaning that the walking sprite has passed the clone, then play the goose sound and say, ‘What's up?’ Click the flag and the right arrow key to test.
The peacock waits to play its sound until the walking sprite passes it.
But, after the walking sprite passes it, it keeps playing the sound until it is off screen.
To make the clone perform the action only once each time it appears on screen, duplicate the wait until code, and stack it on top of the original wait until code.
Tell the clone to wait until its x position is greater than the walking sprite's x position, then wait until its x position is less than the walking sprite's x position.
This makes the clone wait until the sprite passes it before it plays its sound, then it won't play the sound again until the sprite is in front of it and passes it again.
Click the flag and the right arrow key to test again.
Now, it's your turn.
Program the clone to check which costume is displayed.
Check if the costume number equals the costume that will perform the action.
Program an action that the clone will do if the correct costume number is displayed.
Program the clone to wait until the sprite has passed it.
Duplicate the wait until code stack and switch the order of the "x position" blocks.