Skip to content

Commit 9e29a58

Browse files
committed
Sound.fadeOut(duration) will fade the Sound to a volume of zero over the duration given. At the end of the fade the Sound will be stopped and Sound.onFadeComplete dispatched.
Sound.fadeIn(duration, loop) will start the Sound playing, or restart it if already playing, set its volume to zero and then increase the volume over the duration given until it reaches 1. At the end of the fade the Sound.onFadeComplete event is dispatched.
1 parent dd74e3b commit 9e29a58

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Version 2.1.2 - "Whitebridge" - in development
8383
* Rectangle.aabb is a new method that will take an array of Points and return a Rectangle that matches the AABB (bounding area) of the Points (thanks @codevinsky #1199)
8484
* AudioSprite support is now built into the Loader and SoundManager. AudioSprites are like sprite sheets, only they consist of a selection of audio files and markers in a json configuration. You can find more details at https://github.com/tonistiigi/audiosprite (thanks @codevinsky #1205)
8585
* Point.parse will return a new Point object based on the x and y properties of the object given to Point.parse (thanks @codevinsky #1198)
86+
* Sound.fadeOut(duration) will fade the Sound to a volume of zero over the duration given. At the end of the fade the Sound will be stopped and Sound.onFadeComplete dispatched.
87+
* Sound.fadeIn(duration, loop) will start the Sound playing, or restart it if already playing, set its volume to zero and then increase the volume over the duration given until it reaches 1. At the end of the fade the Sound.onFadeComplete event is dispatched.
8688

8789

8890
### Updates

src/sound/Sound.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ Phaser.Sound = function (game, key, volume, loop, connect) {
242242
*/
243243
this.onMarkerComplete = new Phaser.Signal();
244244

245+
/**
246+
* @property {Phaser.Signal} onFadeComplete - The onFadeComplete event is dispatched when this sound finishes fading either in or out.
247+
*/
248+
this.onFadeComplete = new Phaser.Signal();
249+
245250
/**
246251
* @property {number} _volume - The global audio volume. A value between 0 (silence) and 1 (full volume).
247252
* @private
@@ -775,6 +780,74 @@ Phaser.Sound.prototype = {
775780

776781
},
777782

783+
/**
784+
* Starts this sound playing (or restarts it if already doing so) and sets the volume to zero.
785+
* Then increases the volume from 0 to 1 over the duration specified.
786+
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
787+
* and the final volume (1) as the second parameter.
788+
*
789+
* @method Phaser.Sound#fadeIn
790+
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade in.
791+
* @param {boolean} [loop=false] - Should the Sound be set to loop? Note that this doesn't cause the fade to repeat.
792+
*/
793+
fadeIn: function (duration, loop) {
794+
795+
if (typeof duration === 'undefined') { duration = 1000; }
796+
if (typeof loop === 'undefined') { loop = false; }
797+
798+
if (this.paused)
799+
{
800+
return;
801+
}
802+
803+
this.play('', 0, 0, loop);
804+
805+
var tween = this.game.add.tween(this).to( { volume: 1 }, duration, Phaser.Easing.Linear.None, true);
806+
807+
tween.onComplete.add(this.fadeComplete, this);
808+
809+
},
810+
811+
/**
812+
* Decreases the volume of this Sound from its current value to 0 over the duration specified.
813+
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
814+
* and the final volume (0) as the second parameter.
815+
*
816+
* @method Phaser.Sound#fadeOut
817+
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade out.
818+
*/
819+
fadeOut: function (duration) {
820+
821+
if (typeof duration === 'undefined') { duration = 1000; }
822+
823+
if (!this.isPlaying || this.paused || this.volume <= 0)
824+
{
825+
return;
826+
}
827+
828+
var tween = this.game.add.tween(this).to( { volume: 0 }, duration, Phaser.Easing.Linear.None, true);
829+
830+
tween.onComplete.add(this.fadeComplete, this);
831+
832+
},
833+
834+
/**
835+
* Internal handler for Sound.fadeIn and Sound.fadeOut.
836+
*
837+
* @method Phaser.Sound#fadeComplete
838+
* @private
839+
*/
840+
fadeComplete: function () {
841+
842+
this.onFadeComplete.dispatch(this, this.volume);
843+
844+
if (this.volume === 0)
845+
{
846+
this.stop();
847+
}
848+
849+
},
850+
778851
/**
779852
* Destroys this sound and all associated events and removes it from the SoundManager.
780853
*

0 commit comments

Comments
 (0)