Please note: This theme has not been updated to reflect Scratch 3.0. Visit this help article for more info.
Transcript

In this add-on we will write code that plays music when the user presses an arrow key.

To start, select the walking sprite and add music or a sound clip.

You can upload music or choose a sound clip from the Scratch library.

(upbeat music)

Program the sound to play on repeat when the flag is clicked using the Forever, Play Sound Until Done, and When Flag Clicked blocks.

Next, set the volume to 100 when the right arrow key or the left arrow key is pressed.

Add another When Flag Clicked event and another Forever loop.

Inside the Forever loop, add an If Else block.

Your program will say, "If left or right arrow key is pressed, "then set volume to 100%."

Add an Or block and inside this put a Key Right Arrow Pressed block on one side and a Key Left Arrow Pressed block on the other side.

In the Then portion of the block, add a Set Volume To block and set the volume to 100%.

Next, set the volume to 0% when the right key or the left arrow key is not pressed.

Add a Set Volume block to the Else portion of the If Else block and change the value to 0%.

Click the flag and the right arrow key to test.

(upbeat music)

The music plays.

Stop pressing the arrow key.

The music stops.

Well, it doesn't really stop, but you can't hear it anymore.

(upbeat music)

Press the left arrow key.

(upbeat music)

The music starts again.

Great.

(upbeat music)

The music plays when the left or right arrow keys are pressed, then becomes inaudible when they are not pressed.

(upbeat music)

There is no easy way to make the music pause in Scratch but this solution makes it so that the music does not start over from the beginning every time the user presses the right or left arrow keys.

Now it's your turn.

Upload a sound or choose a sound from the Scratch library.

Program the sound to play on repeat when the flag is clicked.

Set the volume to 100% when the right or left arrow keys are pressed.

Set the volume to 0% when the right or left arrow keys are not pressed.

Choose an Add-On

Walk to the Beat

Play music when the character moves.

Passing By

Program the objects being passed to respond to the character.

Scenery Change

Set costumes to a random color when they appear.