0
votes

Everything I've looked up makes this seem so simple, yet I can't get it to work for the life of me.

I have a simplistic flash game that requires a menu to start the game, return to the menu and mute the sound, and I have two different sound files: one that plays on the menu screen and one that plays in the actual game.

I'm trying to control them so that the game music starts when the game is loaded, and stops when you return to the menu. But the game music continues playing when you return to the menu, despite me giving the stop() command.

I can get the stop() command to work once (when I exit the menu and go to the game) but it ONLY ever works once. I need it to stop the music every time I call stop(), not just the very first time.

        public var GameMusic:Sound; 
        public var MenuMusic:Sound;

        public var sc1:SoundChannel;
        public var sc2:SoundChannel;

        GameMusic = new Sound();
        GameMusic.load(new URLRequest("GameMusic.mp3"));

        MenuMusic = new Sound();
        MenuMusic.load(new URLRequest("MenuMusic.mp3"));

        sc1 = GameMusic.play(0, 99);
        sc2 = MenuMusic.play(0, 99);

        sc1.stop();

That's what I have at the beginning. As you can see, I immediately tell GameMusic to stop, because for some reason Flash forces you to play all sound at the beginning if you want to use sound channels. Stopping GameMusic works this time; only the menu music is loaded, as it should be. But later when I return to the menu, it continues playing. In other words, when my code loads the menu again, it calls sc1.stop() but the music continues playing anyway. What am I doing wrong?

2

2 Answers

0
votes

based on this website tutorial http://www.republicofcode.com/tutorials/flash/as3sound/

it should be:

var muted:Boolean = false;//share this var with layer

var GameMusic:Sound = new Sound();
var MenuMusic:Sound = new Sound();
var sc1:SoundChannel = new SoundChannel();
var sc2:SoundChannel = new SoundChannel();

if(!muted){
    GameMusic.load(new URLRequest("GameMusic.mp3"));
    MenuMusic.load(new URLRequest("MenuMusic.mp3"));
    sc1 = GameMusic.play();
    sc2 = MenuMusic.play();
}else{
    sc1.stop();
    sc2.stop();
}
0
votes

What am I doing wrong?

Working with main timeline and navigation by the help of frames.

I assume you lose correct reference on the SoundChannel.