• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Flash 6 - AS2.0 - how to replace a currently playing sound with another sound - attachSound()

I have a script that is intended to play a different sound depending on which button is pressed. Currently the way it is working, when you press the first button, the sound plays fine. When you press a different button, instead of the sound switching to the new sound, you hear both sounds playing. When you click a third button, that sound is added to the mix.

The entire script needs to work from ActionScript alone (nothing on the timeline), using sounds in the library exported as linkages for ActionScript.

Here is the methodology I have been following:

// in global scope, define the sound object
var mySound:Sound = new Sound();

// when the button is pressed, it calls the function to play the appropriate sound
function playNewSound(whichSound:String):Void {
  mySound.attachSound(whichSound);
  mySound.setVolume(80);
  mySound.start(0);
  mySound.onSoundComplete = function(){ mySound.start(0); // to loop the sound }
}

//-----snip

I really thought that attachSound replaced the currently playing sound with the new sound. Was I wrong about this? How is it that a single Sound object can be simultaneously playing 2 or more sounds?

Please note that this solution must work for FlashPlayer 6.

Looking forward to your expert replies!

Tom
0
tomaugerdotcom
Asked:
tomaugerdotcom
1 Solution
 
LocoTechCJCommented:
Add a mySound.stop before loading a sound.

function playNewSound(whichSound:String):Void {
  mySound.stop
  mySound.attachSound(whichSound);
  mySound.setVolume(80);
  mySound.start(0);
  mySound.onSoundComplete = function(){ mySound.start(0); // to loop the sound }
}


LTCJ
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now