title: AS3. How can I avoid sound overlapping?
body: <p>I am working on a flash project in which I have thirteen different music files.  I want the user to be able to choose which song is playing by clicking on its corresponding button.  When the user clicks on one of the thirteen buttons, I want the current song to stop and the new song to play from the beginning so that no sound overlaps.  </p>
  
  <p>When I test my movie I find that, initially, some buttons stop the other music, but some don't and the new music plays over the current music (I'm sure the code for each of the buttons is identical except where there are instance names)  The buttons that do initially work, stop working after a few clicks.  The buttons do everything I need, except for stopping the music that is already playing.</p>
  
  <p>I used a tutorial to learn how use ActionScript code play and control sound files from the library, but my code doesn't work properly for stopping the sound files.  I'm not sure why this is so I will post some code here.  I have quite a lot of code since there are 13 different buttons so I will only post the code for one button.  The code for the rest of the buttons is the same except the number suffixes are changed to be specific to each button.  All the code is on frame 1 of the same symbol.</p>
  
  <pre><code>var music1Play:Music1 = new Music1();
  
  var music1Channel:SoundChannel = new SoundChannel();
  
  btnPiece1.addEventListener(MouseEvent.CLICK, playMusic1);
  
  function playMusic1(e:MouseEvent):void
  
  {
       music2Channel.stop();
       music3Channel.stop();
       music4Channel.stop();
       music5Channel.stop();
       music6Channel.stop();
       music7Channel.stop();
       music8Channel.stop();
       music9Channel.stop();
       music10Channel.stop();
       music11Channel.stop();
       music12Channel.stop();
       music13Channel.stop();
  
       btnPiece1.mouseEnabled = false;    /*Disables button to avoid double click*/
       btnPiece1.mouseChildren = false;
  
       btnPiece2.mouseEnabled = true;    /*Enables the other buttons*/
       btnPiece3.mouseEnabled = true;
       btnPiece4.mouseEnabled = true;
       btnPiece5.mouseEnabled = true;
       btnPiece6.mouseEnabled = true;
       btnPiece7.mouseEnabled = true;
       btnPiece8.mouseEnabled = true;
       btnPiece9.mouseEnabled = true;
       btnPiece10.mouseEnabled = true;
       btnPiece11.mouseEnabled = true;
       btnPiece12.mouseEnabled = true;
       btnPiece13.mouseEnabled = true;
  
       music1Channel = music1Play.play(); 
  
       music1Channel.addEventListener(Event.SOUND_COMPLETE, loopMusic1);
  }   
  
  function loopMusic1(e:Event):void
  
  {
       music1Channel = music1Play.play(); 
       music1Channel.addEventListener(Event.SOUND_COMPLETE, loopMusic1);
  }
  </code></pre>
  
  <p>Please let me know what is wrong with the code or suggest another method I could use.  </p>
  
  <p>Thankyou,</p>
  
  <p>Chris</p>