Let's add some music:
Hopefully, you chose a sound or song you liked. Each time we clicked the flag, the music played forever—or as long as the ball did not hit the red stripe. It was otherwise independent of the ball or the paddle, which is why we associated the script with the stage.
If your music didn't play based on the exercise, make sure you added the script to the stage, not the paddle or ball sprites.
When we want to iterate through a script an infinite number of times, we can use a forever block. Sound is only one example. Another example includes moving across the stage while switching between costumes, which can be included in a forever loop to give the appearance of walking or running. You could also apply a graphical effect to a sprite every five seconds.
The length of time it takes to complete one trip through the loop depends on the code that's included in the loop. For example, if we play a seven minute song, the loop repeats once every seven minutes.
The forever loops can be as complicated as we need them to be. We can nest other forever loops, repeat blocks, and multi-block scripts.
Find a sound that might be suitable for a game over sound, and play it when the ball hits the red stripe.
Which of the following resources can you use to import a sound into your project?
We've already seen a few instances of a forever loop, and we know that the code inside the forever loop runs as long as the project runs. Adding the background music to the game is an example. Click the flag, and the music stops. If the ball hits the red stripe, the entire game stops.
Sometimes, we may want to run a block of code only when a certain condition is met. Let's take a look at a conditional loop using the forever if block.
18.223.172.132