Discuss Scratch

leonyaleonya
New to Scratch
2 posts

Toggling sound with a mouse click

I'm new to Scratch and I'm trying to get a sound to toggle on/off when a particular sprite is clicked, but something is not connecting. I'm hoping someone can help me spot the problem.

Here's what I've written:

when green flag clicked
set [ On] to 0
if <[On = 1]> then
forever
play sound [Pluck]
wait (1) secs
end



else
stop all sounds
end

when this sprite clicked
if <[On = 0]> then
set [On] to 1


else
set [On] to 0

end

Here's the result:
  • I made the “On” variable visible on my stage, and I've verified the variable toggles between 0 and 1 when I click the sprite.
  • I've also tried initializing the variable to 1 instead 0 on startup, and have shown that the sound successfully plays at startup when On is initialized to 1 (and doesn't play at startup when On is initialized to 0).
  • But what's not connecting is that that whatever state the sound is in at startup, it's staying that way, regardless of how often I toggle the On variable between 0 and 1 by clicking the sprite.
What am I missing?
TheLogFather
Scratcher
1000+ posts

Toggling sound with a mouse click

You have your sound playing in a forever loop, so there is nothing that can make it stop once it has started.

Try this:

when GF clicked
set [on v] to [0]
when this sprite clicked
set [on v] to ((1)-(on)) // toggles 'on' between zero and one
if <(on)=[1]> then
forever
play sound [pluck v] until done // see note below!
wait (1) secs // whatever gap you need between plucks
end
end

The way Scratch treats “when clicked” hat block scripts is… well… ‘interesting’…


When you click, if the script was still running from a previous click then the script gets terminated and will restart. That means the ‘forever’ loop in there *will* end if it's running, so the sound will stop.

Also, if the sound is being played using “until done” then that sound will *also* get stopped immediately. However, if it was just a “play sound” block (no ‘until done’) then the sound will continue to play to its end, even though the script itself was terminated and will not finish the subsequent “wait” block.

You can use that difference to choose whether you want the sound to continue playing to its end even when clicked again (don't use “until done”), or instead have the sound ‘cut off’ straight away when clicked again (use “until done”).


The really great advantage to doing it like this is that it doesn't need a “stop all sounds” block, so it doesn't interfere at all with any other sounds that may be playing…

Yeah, bit complicated, but hope it makes some sense!

Last edited by TheLogFather (March 14, 2017 10:29:42)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

leonyaleonya
New to Scratch
2 posts

Toggling sound with a mouse click

Thanks! I'll try that. I ended up solving it like this. It worked, but didn't seem like the most elegant solution. And it seemed odd that I'd have to take an extra step of broadcasting the click of the sprite.

when green flag clicked
set [On] to [0]

when this sprite clicked
if <(On) = [0]> then
set [On v] to [1]
else
set [On v] to [0]
end
broadcast [Toggle v]

when I receive [Toggle v]
if <(On) = [1]> then
forever
play sound [Pluck v]
wait (1) secs
else
stop all sounds
end
end
DadOfMrLog
Scratcher
1000+ posts

Toggling sound with a mouse click

leonyaleonya wrote:

Thanks! I'll try that. I ended up solving it like this. It worked, but didn't seem like the most elegant solution. And it seemed odd that I'd have to take an extra step of broadcasting the click of the sprite.

when green flag clicked
set [On] to [0]

when this sprite clicked
if <(On) = [0]> then
set [On v] to [1]
else
set [On v] to [0]
end
broadcast [Toggle v] // no need for this –just join script below straight on

when I receive [Toggle v]
if <(On) = [1]> then
forever
play sound [Pluck v] // sound still plays to end on 2nd click without "stop all sounds", unless use "until done"
wait (1) secs
else
stop all sounds // no need for this –second click will terminate the "forever" loop
end
end

That's basically the same as I had in my post above – except that the broadcast is not doing anything useful the way you've scripted it, and you don't need the “stop all sounds” (unless your “pluck” sound goes on for a while, in which case you can instead use “play until done”, and it'll get stopped immediately on the second click without any need for “stop all sounds”).

Last edited by DadOfMrLog (March 14, 2017 18:53:39)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

t1nyturtl3_Scratch
Scratcher
3 posts

Toggling sound with a mouse click

Wow that worked better than expected, i used it for a shop menu clicker :)
Thanks

Last edited by t1nyturtl3_Scratch (March 9, 2019 09:33:11)

deck26
Scratcher
1000+ posts

Toggling sound with a mouse click

t1nyturtl3_Scratch wrote:

Wow that worked better than expected, i used it for a shop menu clicker :)
Thanks
No need to necropost to say an old post helped you - all it does is bring that topic back to the top of the forum for no purpose.
DudeNamedJude
Scratcher
2 posts

Toggling sound with a mouse click

How did you get the forever or else block??
imfh
Scratcher
1000+ posts

Toggling sound with a mouse click

DudeNamedJude wrote:

How did you get the forever or else block??
Welcome to the Scratch Forums! This topic is from 2017. Posting on it brings it up to the front page and is called necroposting. Try to avoid doing it in the future.

The forever or else block does not exist. It is actually a typo. It should look like this instead:

when I receive [Toggle v]
if <(On) = [1]> then
forever
play sound [Pluck v] // sound still plays to end on 2nd click without "stop all sounds", unless use "until done"
wait (1) secs
end
else
stop all sounds // no need for this –second click will terminate the "forever" loop
end

Scratch to Pygame converter: https://scratch.mit.edu/discuss/topic/600562/
JimmiesAndTheMakers
Scratcher
15 posts

Toggling sound with a mouse click

when green flag clicked
move (10) steps
say [hey]
pen up
pen down<touching [nano gets grounded v] ?>

when gf clicked
join JimmiesAndTheMakers :: events

when JimmiesAndTheMakers joined :: events hat
forever
imagine ngg :: motion
create projects :: sound
import assets :: operators
code assets :: looks
share projects :: control
end

JimmiesAndTheMakers :: pen // Follow now

Powered by DjangoBB