Copy link to clipboard
Copied
I have a simple game I'm making and in it I want music to play for a minute and then fade out and then go to another frame. Here is the relevant code:
var songReq:URLRequest = new URLRequest("name.mp3");
var song:Sound = new Sound();
song.load(songReq);
var thesong:SoundChannel = new SoundChannel();
function startGameTimer():void {
trace("GAME TIMER STARTED");
var gameTimer:Timer = new Timer(60000, 1);//1min
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endGame);
gameTimer.start();
}
function endGame(e:TimerEvent){
trace("GAME TIMER ENDED");
thesong.stop();///want this to fade instead
var endTimer:Timer = new Timer(4000, 1);//fade out before transition
endTimer.addEventListener(TimerEvent.TIMER_COMPLETE,changeFrames);
endTimer.start();
}
function changeFrames(e:TimerEvent){
gotoAndStop(5);
}
what do I add/change to make the music fade out instead of suddenly stopping? I've been told I do something with tween but I don't know how that works if someone can help me out. Thank you
...import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.media.Sound;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var songReq:URLRequest = new URLRequest("name.mp3");
var song:Sound= new Sound();
song.load(songReq);
var thesong:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
startGameTimer();
function startGameTimer():void {
trace("GAME TIMER STARTED");
thesong = song.play();
sTransfo
Copy link to clipboard
Copied
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.media.Sound;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var songReq:URLRequest = new URLRequest("name.mp3");
var song:Sound= new Sound();
song.load(songReq);
var thesong:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
startGameTimer();
function startGameTimer():void {
trace("GAME TIMER STARTED");
thesong = song.play();
sTransform.volume = 0.5;
thesong.soundTransform = sTransform;
var gameTimer:Timer = new Timer(2000, 1);//1min
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endGame);
gameTimer.start();
}
function endGame(e:TimerEvent){
trace("GAME TIMER ENDED");
//thesong.stop();///want this to fade instead
var tween:Tween = new Tween(sTransform, "volume", Regular.easeOut, sTransform.volume, 0, 1, true);
tween.addEventListener(TweenEvent.MOTION_CHANGE, function():void
{
thesong.soundTransform = sTransform;
});
var endTimer:Timer = new Timer(4000, 1);//fade out before transition
endTimer.addEventListener(TimerEvent.TIMER_COMPLETE,changeFrames);
endTimer.start();
}
function changeFrames(e:TimerEvent){
gotoAndStop(5);
}
Copy link to clipboard
Copied
thank you!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now