Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Fade out music after playing for a minute AS3

Community Beginner ,
Dec 04, 2017 Dec 04, 2017

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

TOPICS
ActionScript
756
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 04, 2017 Dec 04, 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();

    sTransfo

...
Translate
Community Expert ,
Dec 04, 2017 Dec 04, 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 04, 2017 Dec 04, 2017
LATEST

thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines