StanLee Posted December 10, 2014 Share Posted December 10, 2014 Hello, I want to play a sound everytime an enemy dies in my game. As far as I have studied the documentation and the code, an added sound is not destroyed when it is finished playing. Therefore I want to destroy the sound myself after it has finished. To do this I am adding a listener to the "onStop" event of a sound. But everytime a sound gets destroyed I get the following error message: Uncaught RangeError: Maximum call stack size exceededMy code:function preload() { game.load.audio('explosion', 'audio/explosion.wav');}/** Callback function for collision**/function monsterCollision(){ ... var expl = game.add.audio('explosion', 0.1, false); expl.play(); expl.onStop.add(function(){ expl.destroy(); }, game); ...}Kind regards,Stanlee Link to comment Share on other sites More sharing options...
Wavertron Posted December 11, 2014 Share Posted December 11, 2014 Dont create the audio everytime it needs to be played. Instead just do game.audio.add(...) once for each unique sound in your create function. Then you only need to call sound.play() during collision and don't worry about destroying it. Link to comment Share on other sites More sharing options...
StanLee Posted December 11, 2014 Author Share Posted December 11, 2014 The problem is when I only create one sound and play it everytime on a collision, I am not able to hear the same sound multiple times simultaneously. There will be only one sound which starts playing from the beginning on everytime there happens a collision. Link to comment Share on other sites More sharing options...
Wavertron Posted December 11, 2014 Share Posted December 11, 2014 Hmmmm. Ok, I havent played around too much with multiple sounds firing at once. Perhaps create a pool of say 3 sound objects for your one unique collision sound.Then just do a check on sound1.isPlaying, and if the first is playing, check the second one, second one playing, check the third.I dont know the context of your game but if they are fairly short sounds having a pool of 3 or so should be sufficient.A person probably can't distinguish the difference once a few are playing. Alternatively, what if you just stop and restart the sound if its playing - will depend on the sound effect itself, but that might work ok or better than overlapping sounds. Link to comment Share on other sites More sharing options...
Recommended Posts