2012-10-22 73 views
0

我有這個背景音樂在菜單上播放。每當玩家選擇「開始」按鈕(startBtn)時,我想讓它淡出。你是怎樣做的? 這裏是我的代碼:ActionScript 3 Flash CS6 - 淡出音頻

import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.system.fscommand; 
import flash.media.Sound; 
import flash.media.SoundChannel; 
import flash.net.URLRequest; 
import flash.events.Event; 
import flash.media.SoundTransform; 
import flash.events.MouseEvent; 

//Declare MUSIC Variables 
var musicSelection:Sound; 
var musicChannel:SoundChannel; 
var musicTrack:String = "music/dearly_beloved.mp3"; 
var musicVolume:Number=0.2; 
/*var isMuted = false;*/ 

loadMusic(); 
function loadMusic():void 
{ 
    //first stop old sound playing 
    SoundMixer.stopAll(); 
    musicSelection = new Sound(); 
    musicChannel = new SoundChannel(); 
    //create and load the required soun 
    musicSelection.load(new URLRequest(musicTrack)); 
    //when loaded - play it 
    musicSelection.addEventListener(Event.COMPLETE, musicLoaded); 
} 

function musicLoaded(evt:Event):void 
{ 
    //finished with this listener 
    musicSelection.removeEventListener(Event.COMPLETE, musicLoaded); 
    //play music 
    playMusic(); 
} 

function playMusic():void 
{ 
    //play the random or selected music 
    musicChannel = musicSelection.play(); 
    //setting the volume control property to the music channel 
    musicChannel.soundTransform = new SoundTransform(musicVolume, 0); 
    //but add this one to make repeats 
    musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain); 
} 

function playAgain(evt:Event):void 
{ 
    // remove this listener and repeat 
    musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain); 
    playMusic(); 
} 

,這是第二幀代碼:

import flash.events.Event; 
import flash.media.SoundTransform; 

stop(); 

optionBtn.addEventListener(MouseEvent.CLICK, goToOption) 
function goToOption(event:MouseEvent):void 
{ 
    nextFrame(); 
} 

startBtn.addEventListener(MouseEvent.MOUSE_DOWN, stopMusic); 
function stopMusic(event:MouseEvent):void 
{ 
    musicChannel.soundTransform = new SoundTransform(musicVolume, 0); 
    while (musicVolume > 0) 
    { 
     musicVolume--; 
    } 
} 

quitBtn.addEventListener(MouseEvent.MOUSE_DOWN, closeGame); 
function closeGame(event:MouseEvent):void 
{ 
    fscommand("quit"); 
} 

回答

0

使用while ...只會降低音樂的瞬間 - 在一個幀。你需要做的是添加ENTER_FRAME事件,並在其中,減少音樂。事情是這樣的:

addEventListener(Event.ENTER_FRAME, onLoop); 

var _oldTime:Number = 0; 
private function onLoop(e:Event):void 
{ 
    var resultTime:Number = getTimer() - oldTime; 
    if(resultTime > 200) 
    { 
     musicVolume -= 0.1; 
     oldTime += resultTime; 
     if(musicVolume <= 0) 
     { 
     removeEventListener(Event.ENTER_FRAME,onLoop); 
     } 
    } 
} 

根據你想要的速度,嘗試不同的值,降低音量,或者減少(結果時間> 200 - > 200毫秒)