+ 1

Why sound is not playing multiple time check line 141 other sound codes on top

The problem is sound is playing after ending but I want to play sound with every fireworks explode https://sololearn.com/compiler-playground/WMacTBEScLTF/?ref=app

8th Feb 2025, 11:51 AM
Sword 🪄
Sword 🪄 - avatar
3 ответов
+ 4
Sound is playing every time but with a delay 🤔 Lemme see if I can help you (I don't think I will be able to cuz I have gone rusty will js)
8th Feb 2025, 12:38 PM
✧Bhavik(Avery)✧
✧Bhavik(Avery)✧ - avatar
+ 2
Hey Sword 🪄! Add a soundPlayed flag to ensure the sound plays only once per particle explosion. You can do it by applying condition on your explode method. For your help I do this just replace your explode method with this and it works. Enjoy coding 🤘 explode() { if (this.exploded) { sound[Math.floor(Math.random() * sound.length)].src.play(); this.exploded = false; // Ensures sound plays only once } this.alpha -= 0.005; this.x += this.vx; this.y += this.vy; this.velocity *= backForce; this.vy += this.velocity; this.exploded = false; }
8th Feb 2025, 4:04 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 1
Sword 🪄 maybe this? I applied `ᴴᵗᵗየ 's suggestion and tweaked the object pool implementation. two sound objects seems to be enough, though. just alternately pull one out, play it, then put it back. I also ditched the random index call for the soundpool because it seemed unnecessary and potentially problematic. I also renamed this.exploded to this.notExploded because it feels like a more logical name for the particle state. https://sololearn.com/compiler-playground/WZS8T98NS240/?ref=app
9th Feb 2025, 1:24 AM
Bob_Li
Bob_Li - avatar