Skip to main content
jbell2
Participating Frequently
December 4, 2017
Answered

Fade out music after playing for a minute AS3

  • December 4, 2017
  • 1 reply
  • 807 views

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

This topic has been closed for replies.
Correct answer JoãoCésar17023019

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);

}

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
December 4, 2017

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);

}

jbell2
jbell2Author
Participating Frequently
December 4, 2017

thank you!