Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

arrow_back Switch the Background Track

Transcript

In this add-on you will add another background track for the DJ sprite to mix and key press event so the user can switch the track.

To add another track, select the DJ sprite, click on the sounds tab, and select the sound from the sound library.

This example will use the sound Techno2 for the second background track.

Next, go to the code tab, add to and tweak your code so that the user can play one background song at a time.

To do that, go to the variable menu, click mega variable, and crate a new variable called: song.

Next, add code that will allow the user to toggle back and forth between songs by pressing either the one or two key.

To make that happen, go to the variable menu, select a set songtube block and change the value to one.

From the events menu, drag out a when space key press block and connect it to the soundtube one block.

From the dropdown menu select the one key.

This block set now reads, when one key pressed, set song to one.

Perfect, now duplicate this block stack for the two key, and set the song to two.

To adjust the code that plays the background song, remove the play sound until done block from the forever loop.

In this example the background song is techno.

Techno should play if the value of the variable song is equal to one.

Drag out an if then conditional block from the control menu and insert it into the forever loop.

Then, go to the operators menu, drag out a green equal operator block, and insert it into the diamond shaped conditional slot of the if then block.

From the variable menu, drag out the variable song and place it into the green operator block on the left side of the equal sign.

On the right side of the equal sign, change the value to one.

Place the play sound until done block you initially removed inside the if then statement.

The conditional statement now reads: If song equals one, then play sound until done.

In this example the sound is techno, but George will show the sound that you chose for your project.

To copy these blocks, right click on the if then conditional block and select duplicate.

Place the copy beneath the first if then conditional in your block stack, making sure it is still inside the forever loop.

Change the values on the green operator block so it reads: song equal two.

Click the drop down menu of the play sound block and select the new sound.

In this example, techno2.

Texture code: click the green flag and press the one key, then the two key.

(techno music)

The song should switch after it finishes playing.

When you press the one or two key, you should also see the value in the song variable change to either one or two.

(techno music)

Now it's your turn: Add a second background song to your project from the sound library.

Create a new variable called: song.

Create the codes to set song equal to one when the one key is pressed.

Create the code to set the song equal to two when the two key is pressed.

And lastly, create if then conditionals to play the first track if song equals one, and to play the second track if song equals two, and insert them inside the forever loop for the background sound block stack.

Choose an Add-On
DJ Dancer
Make your DJ dance and twist.
Dancers
Add a dancer sprite that has multiple costumes, and give it some moves.
Light Show
Add a light show to your DJ's performance.
Crowd Pleaser
Program an audience for your DJ.
Switch the Background Track
Switch the background track from within your project.
arrow_backward Back
Next arrow_forward
Instructions
  1. Choose an Add-On, and click "watch" to learn how to build it.
  2. Once you finish one Add-On, try another one below the video!