Discuss Scratch

Tsu_Suki
Scratcher
39 posts

How to make a song loop until a sprite is clicked?

Trying to make the main theme of my video game this one song, but when you get into a different area, I want it to change. The song needs to loop until you get into the next area. I can't use a forever loop and use that for the song, because even when you put “stop all sounds” it'll just keep going. The jist of it is that it's going to transition from a menu theme to a in-game theme. I'm also going to need this script for transitioning to normal themes to more intense themes while in game. Any ideas? I've tried this, but it doesn't work.
when this sprite clicked
stop all sounds
broadcast [area 2 v]
forever
play sound [song 2 v]
end

Last edited by Tsu_Suki (July 1, 2021 05:37:05)

kittensu
Scratcher
53 posts

How to make a song loop until a sprite is clicked?

I'm not sure what you mean, but you mean when a sprite clicked, then it will play a song? Then when the area changes you want it to play another sound? I think I can help maybe. I'll just tell you the coding, First sprite: When flag icon clicked switch to background1 go to *position* Make another section and when flag icon clicked start *sound* wait *whatever seconds* and switch to backdrop2. I split the song a little. Second sprite: Then, after that, wait *whatever seconds* and stop all sounds. Then make another sprite. Take a when flag icon clicked block, and then a hide block. Then, a “when backdrop switches to backdrop2” and then, get a SHOW block. Make sure to code it as the last sprite “when this sprite clicked, start sound,” add the start sound block. Anyway, If you meant, when first sprite clicked, start sound, end, next sprite, but not having to click next sprite, you dont have to put the “when sprite clicked” block on that then. Anyway, heres the second sprite coding: When flag clicked, hide. When backdrop switches to background 2, show. Start *sound* and add a repeat block.

Last edited by kittensu (July 1, 2021 06:24:31)

hedgehog_blue
Scratcher
1000+ posts

How to make a song loop until a sprite is clicked?

You need to stop the sound as well as stopping the forever loop laying the first song. To do this, you could have a variable which is set when you enter the second section, that causes the first song to stop looping.

Based on the code you provided, it would look something like this:
when flag clicked
set [song 1 over v] to [false]
repeat until <(song 1 over) = [true]>
play sound [song 1 v] until done
end
stop all sounds
when this sprite clicked
set [song 1 over v] to [true]
broadcast [area 2 v]
forever
play sound [song 2 v]
end
You could keep chaining these for as many songs as you want, by making more variables and repeat until loops instead of forever loops.

Another way, if you don't want this to interfere with other sounds when “stop all sounds” is run, is to permanently play the sounds in separate sprites (or clones), and set the volume to zero in the sprites which do not need to be playing their song.

when flag clicked //in the song 1 sprite
forever
play sound [song 1 v] until done
end
when flag clicked
forever
if <(song) = [song 1]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the song 2 sprite
forever
play sound [song 2 v] until done
end
when flag clicked
forever
if <(song) = [song 2]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the game
set [song v] to [song 1]
when this sprite clicked
set [song v] to [song 2]
broadcast [area 2 v]
Tsu_Suki
Scratcher
39 posts

How to make a song loop until a sprite is clicked?

hedgehog_blue wrote:

You need to stop the sound as well as stopping the forever loop laying the first song. To do this, you could have a variable which is set when you enter the second section, that causes the first song to stop looping.

Based on the code you provided, it would look something like this:
when flag clicked
set [song 1 over v] to [false]
repeat until <(song 1 over) = [true]>
play sound [song 1 v] until done
end
stop all sounds
when this sprite clicked
set [song 1 over v] to [true]
broadcast [area 2 v]
forever
play sound [song 2 v]
end
You could keep chaining these for as many songs as you want, by making more variables and repeat until loops instead of forever loops.

Another way, if you don't want this to interfere with other sounds when “stop all sounds” is run, is to permanently play the sounds in separate sprites (or clones), and set the volume to zero in the sprites which do not need to be playing their song.

when flag clicked //in the song 1 sprite
forever
play sound [song 1 v] until done
end
when flag clicked
forever
if <(song) = [song 1]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the song 2 sprite
forever
play sound [song 2 v] until done
end
when flag clicked
forever
if <(song) = [song 2]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the game
set [song v] to [song 1]
when this sprite clicked
set [song v] to [song 2]
broadcast [area 2 v]
Thank you so much, this really helped me a lot!
gyuygujg
Scratcher
1 post

How to make a song loop until a sprite is clicked?

this so confusing because i'm clicking on a sprite it does nothing…
MasterAralc
Scratcher
1 post

How to make a song loop until a sprite is clicked?

hedgehog_blue wrote:

You need to stop the sound as well as stopping the forever loop laying the first song. To do this, you could have a variable which is set when you enter the second section, that causes the first song to stop looping.

Based on the code you provided, it would look something like this:
when flag clicked
set [song 1 over v] to [false]
repeat until <(song 1 over) = [true]>
play sound [song 1 v] until done
end
stop all sounds
when this sprite clicked
set [song 1 over v] to [true]
broadcast [area 2 v]
forever
play sound [song 2 v]
end
You could keep chaining these for as many songs as you want, by making more variables and repeat until loops instead of forever loops.

Another way, if you don't want this to interfere with other sounds when “stop all sounds” is run, is to permanently play the sounds in separate sprites (or clones), and set the volume to zero in the sprites which do not need to be playing their song.

when flag clicked //in the song 1 sprite
forever
play sound [song 1 v] until done
end
when flag clicked
forever
if <(song) = [song 1]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the song 2 sprite
forever
play sound [song 2 v] until done
end
when flag clicked
forever
if <(song) = [song 2]> then
set volume to (100)%
else
set volume to (0)%
end
end
when flag clicked //in the game
set [song v] to [song 1]
when this sprite clicked
set [song v] to [song 2]
broadcast [area 2 v]

This makes no sense…
CyclonesPlayer28
Scratcher
10 posts

How to make a song loop until a sprite is clicked?

Tsu_Suki wrote:

Trying to make the main theme of my video game this one song, but when you get into a different area, I want it to change. The song needs to loop until you get into the next area. I can't use a forever loop and use that for the song, because even when you put “stop all sounds” it'll just keep going. The jist of it is that it's going to transition from a menu theme to a in-game theme. I'm also going to need this script for transitioning to normal themes to more intense themes while in game. Any ideas? I've tried this, but it doesn't work.
when this sprite clicked
stop all sounds
broadcast [area 2 v]
forever
play sound [song 2 v]
end


I'm not sure if i understand fully what you are asking but from what i understand I think you could do this you do this



when green flag clicked
set [my variable] to [0]

when green flag clicked
forever
if <(my variable) = [1]> then
stop [this script]



else
play sound [ v]
end
end

when this sprite clicked
set [my variable] to [1]
broadcast [area 2]


Sorry if this isn't what you are asking for

Last edited by CyclonesPlayer28 (May 9, 2024 02:19:46)

NIKI-KOLCHAGOV
Scratcher
500+ posts

How to make a song loop until a sprite is clicked?

CyclonesPlayer28 wrote:

Tsu_Suki wrote:

Trying to make the main theme of my video game this one song, but when you get into a different area, I want it to change. The song needs to loop until you get into the next area. I can't use a forever loop and use that for the song, because even when you put “stop all sounds” it'll just keep going. The jist of it is that it's going to transition from a menu theme to a in-game theme. I'm also going to need this script for transitioning to normal themes to more intense themes while in game. Any ideas? I've tried this, but it doesn't work.
when this sprite clicked
stop all sounds
broadcast [area 2 v]
forever
play sound [song 2 v]
end


I'm not sure if i understand fully what you are asking but from what i understand I think you could do this you do this



when green flag clicked
set [my variable] to [0]

when green flag clicked
forever
if <(my variable) = [1]> then
stop [this script]



else
play sound [ v]
end
end

when this sprite clicked
set [my variable] to [1]
broadcast [area 2]


Sorry if this isn't what you are asking for[/quote]

NIKI-KOLCHAGOV wrote:

I dont quite understand as in one part you are talking about changing scenes but then you want it to stop when its clicked i assume its for a rpg game or something i think you meant when you click a building and it changes the music because you are in a different area perhaps this would work

when I receive [start Music 1 v]
set [stop other music v] to [1]
wait (0.1) secs
set [stop other music v] to [0]
repeat until <(Stop other music) = (1)>
play sound [Music 1 v] until done
end

when I receive [start Music 2 v]
set [stop other music v] to [1]
wait (0.1) secs
set [stop other music v] to [0]
repeat until <(Stop other music) = (1)>
play sound [Music 2 v] until done
end

And when you want to play other music you just broadcast it and it will loop until you broadcast other music
I think this should help

Last edited by NIKI-KOLCHAGOV (May 9, 2024 11:32:01)

Powered by DjangoBB