Jump to content

Destroying Sounds


StanLee
 Share

Recommended Posts

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 exceeded

My 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

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...