如何控制AS3的声音/音乐播放



我正在用AS3制作一款游戏。我想在每个背景中放不同的音乐。

到目前为止,我已经成功地在玩家进入场景时启动音乐。但如果他出去了,音乐还在播放……(我希望当玩家离开一个场景时音乐停止)。

我不明白为什么音乐还在播放。下面是我的代码:
public function newBackground(thisBack:String):void{
            var room = back.currentBack;
            switch (thisBack){
case "maisonExt":
                    var mySound:Sound = new exterieur ();
                    mySound.play ();

我的mp3文件名为"exterieur.mp3"。

如果你能想到什么,那就太好了。

谢谢!

你需要再次停止音乐。如果你不停止播放,它会继续播放:)

当你调用。play时,它将返回一个SoundChannel,它允许你控制播放——改变位置,停止播放,访问SoundTransform,它允许你控制音量。下面是一些示例代码:

public var currentSoundChannel:SoundChannel;
public function newBackground(thisBack:String):void
{
    var room = back.currentBack;
    if (currentSoundChannel != null)
    {
        currentSoundChannel.stop()
    }
    var nextSong:Sound;
    switch (thisBack){
        case "maisonExt":
            nextSong = new exterieur ();
            break;
    }
    currentSoundChannel = nextSong.play();
}

如果你想了解更多关于声音,我会推荐这个教程http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/。这是一个更大的教程的一部分,它教你游戏制作的许多方面,让你从新手到有能力创造游戏。你真的应该去看看