Jump to content

Fade Out sound


Recommended Posts

  • 3 weeks later...

I haven't found a fadeMusic function too so here's what I'm using for now.


In the create function, add some property or variable (ie) shouldfade for detecting if the music should fade.

Then inside the update function, check if the shouldfade property is set to true and decrement the volume by some value.

create: function() {		this.somemusic = this.add.audio('somemusic', 1, true);	this.somemusic.play();	//add property for detecting if the music should be faded out	this.somemusic.shouldfade = false;        //a quick button for testing; starts fading somemusic when clicked        this.somebutton = this.game.add.button(this.game.width/2, this.world.centerY, 'someimage', function() {this.somemusic.shouldfade = true;}, this);},update: function() {	if (this.somemusic.shouldfade == true && this.somemusic.volume > 0) {		this.somemusic.volume -= 0.1;	}},

Other things you can do:

  • Use a global variable and decrement the global volume instead of fading out per song.
  • Make the fade faster or slower by decrementing with larger or smaller values.
  • Make the fade smoother by halving the current volume instead. Keep in mind your volume will only approach but never actually reach 0 with this method. You have to adjust your condition accordingly and stop decrementing at some low volume instead of 0.
this.somemusic.volume = this.somemusic.volume*0.5;
Link to comment
Share on other sites


  • Recently Browsing   0 members

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