Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Change Looping Music?
- bluuberrygutzz
-
Scratcher
2 posts
Change Looping Music?
Hello!!! Currently, I'm making a clicker game as an assignment. I want the music to change depending on certain goals that are met (1K, 10K, etc), while also having that music loop until that point. I plan on having 4 songs included. Currently, I have “music” set as a variable that should change the music. When the clicker detects that, for example, 1K is met, it should send a broadcast to change the music variable and update the music. Right now, I have set up code such as:
Song1 runs fine, but when it's time to swap the music, Song2 continuously loops it's beginning over and over. Can someone please help??
define check_music
forever
if <[(music)] = [1]> then
stop all sounds
repeat until <not <[(music)] = [1]>>
play sound [song1 v] until done
end
end
if <[(music)] = [2]> then
stop all sounds
repeat until <not <[(music)] = [2]>>
play sound [song2 v] until done
end
end
end
Song1 runs fine, but when it's time to swap the music, Song2 continuously loops it's beginning over and over. Can someone please help??
Last edited by bluuberrygutzz (Aug. 30, 2024 01:08:24)
- souleymane2
-
Scratcher
100+ posts
Change Looping Music?
Hey there,
When it comes to solving bugs, the solution is rarely in small bits of code itself. Would you mind sharing the projetc so that I could have a deeper look?
When it comes to solving bugs, the solution is rarely in small bits of code itself. Would you mind sharing the projetc so that I could have a deeper look?
- bluuberrygutzz
-
Scratcher
2 posts
Change Looping Music?
Sure! There are a lot of features unfinished, so do keep that in mind.
https://scratch.mit.edu/projects/1060572535
https://scratch.mit.edu/projects/1060572535
- souleymane2
-
Scratcher
100+ posts
Change Looping Music?
Hey there,
I managed to fix your problem
Although I couldnt exactly find the source of it, I believe it was linked to you running the same custom block multiple times in the music_toggle sprite. Instead heres what I did:
First of all, I wanted to detect when we changed music. This was fairly easy as all I had to do was add wait until x blocks like so:
Now that I could detect when a change was made, I added a forever that would play the music like so:
Finally I stopped all the sounds whenever we chanted music to prevent the music from going on longer then intended.
And this was my final product! No broadcast or custom block needed!
I've remixed your project so that you can see the changes for yourself. Hope this helps!
I managed to fix your problem
Although I couldnt exactly find the source of it, I believe it was linked to you running the same custom block multiple times in the music_toggle sprite. Instead heres what I did:
First of all, I wanted to detect when we changed music. This was fairly easy as all I had to do was add wait until x blocks like so:
when green flag clicked
wait until <(music) = [1]>
wait until <(music) = [2]>
Now that I could detect when a change was made, I added a forever that would play the music like so:
when green flag clicked
forever
if <(music) = [1]> then
play sound [first song v]
end
if <(music) = [2]> then
play sound [second sound v]
end
end
Finally I stopped all the sounds whenever we chanted music to prevent the music from going on longer then intended.
when green flag clicked
wait until <(music) = [1]>
stop all sounds
wait until <(music) = [2]>
stop all sounds
And this was my final product! No broadcast or custom block needed!
when green flag clicked
forever
if <(music) = [1]> then
play sound [first song v]
end
if <(music) = [2]> then
play sound [second sound v]
end
end
when green flag clicked
wait until <(music) = [1]>
stop all sounds
wait until <(music) = [2]>
stop all sounds
I've remixed your project so that you can see the changes for yourself. Hope this helps!
- Discussion Forums
- » Help with Scripts
-
» Change Looping Music?

