1.11M Members

Flash background music need help

 
0
 

My problem is that I have an option page which contains the button to control the background music on and off. it works fine. But when I exit the option page then go back to option page again and play the music again, there will be two sound playing and if I stop it, only one sound will stop and the other one keeps on playing.
Here is my code please help me fix this thing thanks :) By the way Im using AS3.
to illustrate:

> ===================
> [   OPTION       ]        () is the on and off button of my music
> [ MUSIC: ()      ]       when I on the music it plays
> [                ]        then when I go to the main menu, the music plays well
> [ BACK           ]         no problem in this part 
> ===================
> 
> =====================
> [                  ]       in the main menu, music is playing well
> [  MAIN MENU       ]       if I go to the option page again
> [                  ]        ....................
> [ PLAY OPTION ETC  ]
> ===================
> 
> ===================
> [                ]     Here it goes, when I click the on and off button again to stop the music
> [  MUSIC: ()     ]     It didnt stop, instead it plays another music
> [                ]     So my problem is that. Please help me solve this
> [ BACK           ]
> ===================
> 





on_off_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound); // on_off_btn is the instance name

var fl_SC:SoundChannel;

//This variable keeps track of whether you want to play or stop the sound
var fl_ToPlay:Boolean = true; 

function fl_ClickToPlayStopSound(evt:MouseEvent):void
{   
    if(fl_ToPlay)
    {
        var s:Sound = new Sound(new URLRequest("harry.MP3"));
        fl_SC = s.play();
    }
    else
    {
        fl_SC.stop();
    }
    fl_ToPlay = !fl_ToPlay;
}
 
0
 

Because you're creating a new Sound whenever the user clicks play. Create the sound first, outside of the function.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: