实际代码很大程度上取决于您在声音中的加载方式,因此我将为代码编写“框架”。
var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;
var sound1:Sound = /* Load me */
var sound2:Sound = /* Load me */
button1.addEventListener(MouseEvent.CLICK, playSound1);
function playSound1(event:MouseEvent)
{
playSound(sound1);
}
button2.addEventListener(MouseEvent.CLICK, playSound2);
function playSound2(event:MouseEvent)
{
playSound(sound2);
}
function playSound(sound:Sound):void
{
if (currentSound != null)
{
// Stop the current music
currentSoundChannel.stop();
}
if (currentSound == sound)
{
// Stop playing ANY sound
currentSound = null;
currentSoundChannel = null;
}
else
{
// Play a different sound
currentSound = sound;
currentSoundChannel = sound.play();
}
}
自己做一个尝试......当你卡在某个地方时发布具体问题。对于初学者...尝试参考:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html – loxxy