2013-12-09 76 views
0

對不起,我的英文..我有一個代碼爲兩個按鈕來靜音和取消靜音聲音..當我點擊靜音的聲音,它的作品,但當我點擊另一個按鈕,使聲音播放它不工作..任何幫助?如何解除as3中的聲音?

import flash.media.SoundMixer; 

speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound); 
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound); 
var channel2:SoundChannel = new SoundChannel(); 
var clicktoPlay:Boolean = true; 


function speaker2sound(e:MouseEvent):void{ 
    if (clicktoPlay==true){ 
     var snd2:Sound = new Sound; 
     snd2.load(new URLRequest("shakeup.wav")); 
     clicktoPlay=false; 
     channel2 = snd2.play(); 
     speakerb1.visible=true; 
     speakerb2.visible=false; 
     SoundMixer.soundTransform = new SoundTransform(1); 
    } 

    if (clicktoPlay==false){ 
     clicktoPlay=true; 
     speakerb2.visible=true; 
     speakerb1.visible=false; 
     SoundMixer.soundTransform = new SoundTransform(0); 
    } 
    clicktoPlay =!clicktoPlay; 
} 

channel2.addEventListener(Event.SOUND_COMPLETE,soundfin); 

function soundfin(event:Event):void{ 
    clicktoPlay=false; 
    speakerb1.visible=true; 
    speakerb2.visible=false; 
} 
+0

它可能有一些做的事實,你正在做一些奇怪的東西你'clicktoPlay'變量。你檢查它是否爲真,然後將它設置爲false,然後檢查它是否是假的(它總是會是的,因爲如果它最初是真的,你將它設置爲假)。然後,所有這一切,你再次切換。 – Marcela

+0

是的,但當我再次點擊,它是假的,然後我讓clicktoPlay真正.. – Domna

回答

0

我收到了你的代碼有一些小的改動工作:

speakerb1.addEventListener(MouseEvent.CLICK, speaker2sound); 
speakerb2.addEventListener(MouseEvent.CLICK, speaker2sound); 
var channel2:SoundChannel = new SoundChannel(); 
var clicktoPlay:Boolean = true; 
var snd2:Sound; 

function speaker2sound(e:MouseEvent):void{ 
    if (clicktoPlay==true){ 
     if(!snd2){ 
      snd2 = new Sound(); 
      snd2.load(new URLRequest("Kalimba.mp3")); 
      channel2 = snd2.play(); 
     } 
     speakerb1.visible=true; 
     speakerb2.visible=false; 
     SoundMixer.soundTransform = new SoundTransform(1); 
    } 
    else { 
     speakerb2.visible=true; 
     speakerb1.visible=false; 
     SoundMixer.soundTransform = new SoundTransform(0); 
    } 
    clicktoPlay =!clicktoPlay; 
} 

channel2.addEventListener(Event.SOUND_COMPLETE,soundfin); 

function soundfin(event:Event):void{ 
    clicktoPlay=false; 
    speakerb1.visible=true; 
    speakerb2.visible=false; 
} 

channel2.addEventListener(Event.SOUND_COMPLETE,soundfin); 

function soundfin(event:Event):void{ 
    clicktoPlay=false; 
    speakerb1.visible=true; 
    speakerb2.visible=false; 
} 
0

正如Marcela說的,嘗試取出「clicktoPlay =!clicktoPlay;」這一行。並看看它是否有效。 您的代碼現在的方式,直到調用「soundfin」時,無論speaker2sound被調用的頻率如何,clicktoPlay都將保持爲真。