thieberson Posted June 25, 2014 Share Posted June 25, 2014 There is some way to fade in/out a sound in Phaser? I have tried to create a function, but no success. Thanks. Link to comment Share on other sites More sharing options...
mary Posted July 14, 2014 Share Posted July 14, 2014 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 More sharing options...
Recommended Posts