Jump to content

Destroying Sounds


Recommended Posts



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,


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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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