Skip to main content
Participant
July 4, 2011
Answered

music loop fade out by button between scenes

  • July 4, 2011
  • 2 replies
  • 683 views


Hi

I have a infinite music loop and I am trying to have btn_start trigger a fade out on the music along with its original gotoAndPlay.

I have two scenes, "opening" and "animation". btn_start and the following code are in the "opening" scene.

the fade out will play along with the first couple seconds of "animation" since the gotoAndPlay connects to frame 1 of "animation"

Can this be done? Please show me some lights of how to proceed this.

stop();

import flash.net.URLRequest;
import flash.media.Sound;

var url:URLRequest = new URLRequest("subtle.mp3");
var snd:Sound = new Sound(url);
snd.play(0, 17);

btn_start.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {

gotoAndPlay(1, "animation");

}

thanks!!

This topic has been closed for replies.
Correct answer kglad

you can use:


stop();

import flash.net.URLRequest;
import flash.media.Sound;

var fadeRate:Number = .01;  // number between 0 and 1.  the closer to 1, the faster the fade


var url:URLRequest = new URLRequest("subtle.mp3");
var snd:Sound = new Sound(url);
var sc:SoundChannel=snd.play(0, 17);

btn_start.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME,fadesoundF);
gotoAndPlay(1, "animation");

}

function fadesoundF(e:Event):void{

var st:SoundTransform=sc.soundTransform;

st.volume-=fadeRate;

if(st.volume<=0){

this.removeEventListener(Event.ENTER_FRAME,fadesoundF);

}

sc.soundTransform=st;

}

2 replies

kdarkzAuthor
Participant
July 4, 2011

works PERFECT!

millions thanks kglad!

kglad
Community Expert
Community Expert
July 4, 2011

you're welcome.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 4, 2011

you can use:


stop();

import flash.net.URLRequest;
import flash.media.Sound;

var fadeRate:Number = .01;  // number between 0 and 1.  the closer to 1, the faster the fade


var url:URLRequest = new URLRequest("subtle.mp3");
var snd:Sound = new Sound(url);
var sc:SoundChannel=snd.play(0, 17);

btn_start.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME,fadesoundF);
gotoAndPlay(1, "animation");

}

function fadesoundF(e:Event):void{

var st:SoundTransform=sc.soundTransform;

st.volume-=fadeRate;

if(st.volume<=0){

this.removeEventListener(Event.ENTER_FRAME,fadesoundF);

}

sc.soundTransform=st;

}