Discuss Scratch

-Usernameisused-
Scratcher
4 posts

Music switches on different backgrounds

Hi, I would like help on switching music for different backdrops (going to referenced as background 1 and 2), where the sprite would move from background 1 to background 2, which would stop backdrop 1's music and play backdrop 2's music.

ty in advance
PangolinButCodes
Scratcher
43 posts

Music switches on different backgrounds

when green flag clicked
forever
if <(backdrop #) = [costumeNumber]> then
stop all sounds

play sound [sound] until done

else
if <(backdrop #) = [costumeNumber2]> then
stop all sounds
play sound [sound2] until done
end
end
end
This should work.
edit – costumeNumber & costumeNumber2 should be the costumes. For more than 2 backdrops, repeat using the if then else, last one should just be if <> then.
if you think this is too tedious, you can try to make a little more advanced run without screen refresh looping custom block.

Last edited by PangolinButCodes (March 7, 2024 05:22:58)

-Usernameisused-
Scratcher
4 posts

Music switches on different backgrounds

the code didnt work on the main project (specically the forever command), but worked on another project for some reason
also i checked the code for each sprite and it shouldn't have interfered with the script

Last edited by -Usernameisused- (March 7, 2024 05:57:35)

Modz31
Scratcher
72 posts

Music switches on different backgrounds

when green flag clicked
set volume to (100) %
forever
if <(backdrop name) = [Backdrop 1]> then
stop all sounds
play sound [Sound 1 v] until done
end
if <(backdrop name) = [Backdrop 2]> then
stop all sounds
play sound [Sound 2 v] until done
end
end
kiwilover2736
Scratcher
1000+ posts

Music switches on different backgrounds

Modz31 wrote:

when green flag clicked
set volume to (100) %
forever
if <(backdrop name) = [Backdrop 1]> then
stop all sounds
play sound [Sound 1 v] until done
end
if <(backdrop name) = [Backdrop 2]> then
stop all sounds
play sound [Sound 2 v] until done
end
end
I would recommend putting the “If” statements in seperate “Forever” blocks, because otherwise the music will always play until the end before switching, or you could do something like this:

when green flag clicked
forever
if <(backdrop name) = [Backdrop 1]> then
stop all sounds
start sound [Music] ::sound
wait until <not <(backdrop name) = [Backdrop1]>>
end
end

You could repeat this with the other “If” statements too.
Piggo12
Scratcher
1 post

Music switches on different backgrounds

to kiwilover2736, your code actually helped me out in my own project when I was struggling how to work out the same problem! thanks!

Powered by DjangoBB