Admiration

Hello, i'm making a sprite which plays music if you hold down the space key, but i want the music to loop, so if i put in a
play sound [ v] until done
and release the space key, it won't stop, since i put a
stop all sounds
block in the code.

Here's the full code below:

when [space v] key pressed
switch costume to [costumehere v]
if <[costume #] = [2]>() then 
play sound [musichere v] until done



else

wait until <not <key [space v] pressed?>>
switch costume to [startcostume v]
stop all sounds

I'm still a bit new to posting, sorry if the code is jumbled.

plasmamasta

You should use this block
play sound [musichere v]
In 3.0 blocks it is start sound

OwenRich

Well, you use play sound/start sound and put the items in the else section in the if section under the play sound block, that should work.

deck26

The thing to remember is that a block that plays sound until done means that script is stuck at that block until the sound is finished or interrupted. So there's no point expecting the same script to also detect a space a stop a sound that it is playing - you need a separate script to detect the space and stop all sounds so the main script can move on from the ‘play sound until done’ block.

From memory if you press space once and the script is still running pressing the space again won't restart the script. However if you used a broadcast

when [space v] key pressed
broadcast [space-pressed v]
and put your code n the space-pressed receiver the receiver would be restarted when you pressed space and it should be possible to get the effect you want.

mstone326

Here is another method the loop the song and when you release space the song ends by using a clone. Create a clone, the clone plays the song. When space key is released we just delete that clone and the song ends.

https://scratch.mit.edu/projects/321592273/

Edit: I left the clone visible to show how he gets deleted and also to make sure we were creating multiple clones while the space key was pressed.