CSD 20-21 Unit 3 Lesson 17 Level 3

This interactive card example has the code for stopSound first and playSound second. I just wonder if anyone could tell me why you might want to do it in this order.
https://studio.code.org/projects/gamelab/-tuUvqdeUWqCLb5FNl-OjoSC2ShT6DEum8hvVSesoTw

@koliner,

Since the draw loop is executed over and over again, putting the stop sound first will stop the sound (if there is one) that is playing from the previous cycle and start the sound again. If you put the play sound first and then the stop sound, it will start the sound and immediately stop it (faster than you can hear it start) and you won’t hear it at all.

I remixed that project just to test it and sure enough, if I switch the order, the sounds don’t work at all.

By stopping and then starting, the sound plays, the rest of the draw loop is executed and that gives the sound enough time to start playing before the “stop sound” block stops it again (if the space key is pressed) and starts the sound anew.

Hopefully this makes sense! If not, let me know and I’ll take another stab at being more clear.

Mike

I’m still having trouble understanding how sound is handled. This student’s project plays the sound correctly the first Run but then needs the webpage refreshed in order to play correctly again.
(click 10 times on the jack-o-lantern and then the witch flies off and there is a “witch cackle” sound.) Thanks for any enlightenment.

@koliner, In this one, to be honest, I’m not 100% sure why it won’t reset when you restart the game.
It seems I’ve seen something like that before. However, I was able to fix it.

I would suggest putting the “stop sound” command right before the play sound command. This will stop the sound from playing if it is already playing. Then, reset the click variable once it is greater than 9. This will only allow the sound to trigger once (per game). Then, inside that same conditional, once the click >9, I would put the following three commands inside the loop.

  1. click = 0; (reset the click variable)
  2. stop the sound
  3. play the sound.

Once I did this, it worked. Sounds can be tricky and I’m sorry I can’t explain why you have to restart the project you have to get it to work, but I did just make those changes and they do cause it to work.

Hope this helps?

Mike