# playSound in draw frunction

**URL:** <https://forum.code.org/t/playsound-in-draw-frunction/31776>\
**Category:** CS Discoveries\
**Tags:** csd-unit-3-lesson-14\
**Created:** [November 22, 2019, 6:27pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776 "2019-11-22T18:27:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmaletta](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@dmaletta](https://forum.code.org/u/dmaletta)\
**Post date:** [November 22, 2019, 6:27pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/1 "2019-11-22T18:27:26Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/c/c05ba901d7f5a2f3c1f3fa2a9558da0725c8370d.png)

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 22, 2019, 7:54pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/2 "2019-11-22T19:54:30Z")

</div>

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](https://studio.code.org/projects/gamelab/ReY0tMN5630lFjLdJK8DISCF72KdBWgpzvxaLEK64As) 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” 🙂

Michelle

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [November 22, 2019, 8:20pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/3 "2019-11-22T20:20:37Z")

</div>

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.](https://studio.code.org/projects/gamelab/keNQN7sTlIsAXmmhjdIusZZlI6jVnbjxfdXt5gGUieU)

Mike

---

<div class="post-metadata">

**Author:** ![dmaletta](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@dmaletta](https://forum.code.org/u/dmaletta)\
**Post date:** [November 22, 2019, 8:24pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/4 "2019-11-22T20:24:05Z")

</div>

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…  
☹

![image001.jpg](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/d/dfffaed64936d23062673086f6543958de41da01.jpeg)

---

<div class="post-metadata">

**Author:** ![dmaletta](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@dmaletta](https://forum.code.org/u/dmaletta)\
**Post date:** [November 22, 2019, 8:24pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/5 "2019-11-22T20:24:57Z")

</div>

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](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/d/dfffaed64936d23062673086f6543958de41da01.jpeg)

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 22, 2019, 9:30pm UTC](https://forum.code.org/t/playsound-in-draw-frunction/31776/6 "2019-11-22T21:30:07Z")

</div>

Oh good! 🙂
