在动作脚本3中静音取消静音按钮

时间:2013-08-04 08:28:44

标签: actionscript-3

我的动作脚本3项目需要一些帮助。我有一个带声音的按钮。我有一些代码(见下文),当我按下按钮时它会播放声音,如果我再次按下按钮它会停止声音(如静音/取消静音按钮)。问题是,当我按下按钮播放第二次声音时,它会播放两个声音(相同的声音两次),如果按下按钮播放声音的次数相同,则会播放多次相同的声音。能帮我解决一下这个问题吗?谢谢。

   function setMute1(vol){
       sTransform1.volume = vol; 
       SoundMixer.soundTransform = sTransform1;
  }

   var sTransform1:SoundTransform = new SoundTransform(1,0);
   var Mute1:Boolean = true;
   sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);

   function toggleMuteBtn1(event:Event) {
    if(Mute1 === false) {
        Mute1 = true;
        setMute1(0);
     } else {
         Mute1 = false;
         setMute1(1);
     }
  }

2 个答案:

答案 0 :(得分:4)

根据我的理解,您可以通过将其分配到按钮框架来启动声音?您需要通过代码开始声音,以便以良好的方式控制声音。

以下是基于您的代码的工作示例,它会加载外部 mp3 文件。声音通过相同的按钮播放和停止。

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Start/stop sound
function startOrStop()
{
    if (Mute1 === false)
    {
        myChannel.stop();
        setMute1(0);
    }
    else
    {
        setMute1(1);
        myChannel = mySound.play();
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    startOrStop()
}

actionscrip 2 中有一个功能停止所有声音,在动作脚本3 中你不能再这样做了,但是你仍然可以将声音分配给帧。

答案 1 :(得分:1)

此示例将声音静音和取消静音。声音没有停止,只是静音。 此外,声音必须在代码中指定为,而不是在帧中指定。

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Toggle mute on/off
function toggleMute()
{
    if (Mute1 === false)
    {
        setMute1(0);
    }
    else
    {
        setMute1(1);
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    // if not playing, the sound
    if (myChannel.position != 0) {
    } else {
        myChannel = mySound.play();
    }

    toggleMute();
}