playSound in draw frunction

My students are trying to use playSound once a counter threshold is reached (instead of in response to a button)… but the infinite loop sound overlay issue is occurring… is there any way to work around this?

Example:

Maletta,

My first inclination is to have the sound play if click == 20. That narrows the scope of when it is true.

I made a little mini-version without sprites so you can see the code. The sound only plays once.

If that does not work as he intended, can you click SHARE on the project so we can take a look “under the hood” :slight_smile:

Michelle

Michelle does make a good point that if you set the operator to ==, it will only play when the # of clicks is exactly 20, but when it surpasses 20, it will stop playing.

Another approach is to create a variable (exploded) that you set to false at the beginning of the program. Then, when clicks reach 20, it changes that variable to true, plays the sound once and then carries on. Because the sound will only play if exploded is false, it won’t play over and over again once it has been triggered the first time.

Here’s some example code for that solution. If you click on the cat 10 times, it changes into a dog and a sound is made.

Mike

When I run yours it plays repeated over itself… but I noticed you didn’t use ==

I tried using == and it still plays repeatedly over itself…
:frowning:

image001.jpg

NVM… it just worked… I’ve noticed sometimes I or my students have to refresh the page for the code to work as expected…

image001.jpg

Oh good! :slight_smile: