Skip to content

Commit 3728f25

Browse files
committed
SoundManager.remove(sound) now lets you remove a sound from the SoundManager, destroying it in the process.
Sound.destroy will remove a sound and all local references it holds, optionally removing itself from the SoundManager as well.
1 parent e967039 commit 3728f25

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ New Features
8686
* Graphics chaining functions.
8787
* Added Pointer.positionUp which records the last point at which the pointer left the screen (thanks @Cryszon, #676)
8888
* Phaser.Point.centroid static function added to calculate the centroid or midpoint of an array of points (thanks @lewster32, #675)
89+
* SoundManager.remove(sound) now lets you remove a sound from the SoundManager, destroying it in the process.
90+
* Sound.destroy will remove a sound and all local references it holds, optionally removing itself from the SoundManager as well.
8991

9092

9193
Bug Fixes

src/sound/Sound.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ Phaser.Sound.prototype = {
662662

663663
/**
664664
* Stop playing this sound.
665+
*
665666
* @method Phaser.Sound#stop
666667
*/
667668
stop: function () {
@@ -697,6 +698,38 @@ Phaser.Sound.prototype = {
697698
this.currentMarker = '';
698699
this.onStop.dispatch(this, prevMarker);
699700

701+
},
702+
703+
/**
704+
* Destroys this sound and all associated events and removes it from the SoundManager.
705+
*
706+
* @method Phaser.Sound#destroy
707+
* @param {boolean} [remove=true] - If true this Sound is automatically removed from the SoundManager.
708+
*/
709+
destroy: function (remove) {
710+
711+
if (typeof remove === 'undefined') { remove = true; }
712+
713+
this.stop();
714+
715+
if (remove)
716+
{
717+
this.game.sound.remove(this);
718+
}
719+
720+
this.markers = {};
721+
this.context = null;
722+
this._buffer = null;
723+
this.externalNode = null;
724+
this.onDecoded.dispose();
725+
this.onPlay.dispose();
726+
this.onPause.dispose();
727+
this.onResume.dispose();
728+
this.onLoop.dispose();
729+
this.onStop.dispose();
730+
this.onMute.dispose();
731+
this.onMarkerComplete.dispose();
732+
700733
}
701734

702735
};

src/sound/SoundManager.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Phaser.SoundManager = function (game) {
5757
/**
5858
* @property {array} _sounds - An array containing all the sounds
5959
* @private
60-
* @default The empty array.
6160
*/
6261
this._sounds = [];
6362

@@ -364,8 +363,34 @@ Phaser.SoundManager.prototype = {
364363

365364
},
366365

366+
/**
367+
* Removes a Sound from the SoundManager. The removed Sound is destroyed before removal.
368+
*
369+
* @method Phaser.SoundManager#remove
370+
* @param {Phaser.Sound} sound - The sound object to remove.
371+
* @return {boolean} True if the sound was removed successfully, otherwise false.
372+
*/
373+
remove: function (sound) {
374+
375+
var i = this._sounds.length;
376+
377+
while (i--)
378+
{
379+
if (this._sounds[i] === sound)
380+
{
381+
this._sounds[i].destroy(false);
382+
this._sounds.splice(i, 1);
383+
return true;
384+
}
385+
}
386+
387+
return false;
388+
389+
},
390+
367391
/**
368392
* Adds a new Sound into the SoundManager and starts it playing.
393+
*
369394
* @method Phaser.SoundManager#play
370395
* @param {string} key - Asset key for the sound.
371396
* @param {number} [volume=1] - Default value for the volume.

0 commit comments

Comments
 (0)