1

I'm making a game with AS3. I would like to put a different music in each background.

So far, I've succeed to start the music when the player enters in a scene. But if he goes out, the music is still playing...(I want the music to stop when the players leave a scene).

I don't understand why the music is still playing.. Here's my code :

public function newBackground(thisBack:String):void{
            var room = back.currentBack;
            switch (thisBack){
case "maisonExt":
                    var mySound:Sound = new exterieur ();
                    mySound.play ();

My mp3 file is named "exterieur.mp3".

If you can think of anything it would be a great help.

Thanks !

4

1 回答 1

1

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

当你调用 .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/。它是一个更大的教程的一部分,它教你游戏制作的许多方面,带你从新手到能够创建游戏。你真的应该检查一下。

于 2013-06-16T11:23:54.497 回答