Sound not stopping on interactive card

csdunit3
javascript
csdiscoveries
gamelab
lesson14

#1

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


#2

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


#3

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


#4

The code can be found here

Thanks,
Ross


#5

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.


#6

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


#7

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.


#8

Mike,

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