1
votes

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 !

1
Code that you provide it's not JavaScript so... add another tagsGivi

1 Answers

1
votes

You need to stop the music again. If you don't stop it, it will keep on playing :)

When you call .play it will return a SoundChannel which lets you control the playback -- changing position, stopping the playback, and accessing the SoundTransform which lets you control the volume. Here is some example code for you:

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

If you want to learn more about sound, I would recommend this tutorial http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/. It's part of a larger tutorial which teaches you many aspects of game making, taking you from novice to capable of creating a game. You should really check that out.