Sound not stopping on interactive card

I have a student who has put sound in and only wants it to play when a condition is met. She has put the stop sound in, but the sound does not stop. Has anyone run into this same problem? At one point the computer was glitching and even when the program wasn’t running the sound was still playing.

Thoughts? Ideas? I will submit her code if I need too.
Thanks,
Ross

My issue is similar to this, but not exactly the same.:smirk:

It would be helpful to see the code so someone can debug or give suggestions on how to make this work better.

The code can be found here

Thanks,
Ross

Hi Ross,
It looks like the student’s idea is to make the sound play when “burger1” is invisible. The way the draw loop works is that all of the code inside is executed every frame. That means that if any point the burger is invisible the song will play (or keep playing). The problem is that the condition the student is checking (burger1.visible === false) continues to be true even after the sound is done playing. In lesson 16, students work on a game where a bunny gets points if it is touching the carrot. Unless they move the carrot out off the screen, the bunny just continues to get points repeatedly. (Code here.) This is a similar behavior to that.
The challenge is going to be finding the right conditions that are true when the sound should start playing, but false all other times. Sometimes you can create a variable to track whether something has happened yet and use that to make sure it only happens exactly one time.

That is what I was thinking, she needed to create a variable to play one time. In the code as of right now there is not stop sound, because we ran out of time to play around with it.

Any thoughts on how to create a variable for a sound? Some how variable where Soundplay=1

Something along those lines should work.

Outside the draw loop:
var soundPlay = true;

inside the draw loop:
if (burger1.visible === false && soundPlay) { playSound("sound://category_animals/crocodile.mp3", true); soundPlay=false; }

This solution might work for the student, but if you wanted the sound to be able to play at multiple times through the game, you’d have to do something different.

Mike,

Thanks for your help. Didn’t work, unfortunately. I am going to have her make the burgers reappear then the sound will stop.

OK, this actually worked for me. I will mention in the code above, the sound loop is set to “true,” so the sound will repeat after it is played. If you want to sound to only play once, set that to false. That’s what my student wanted in their code - one play of the sound.