Pregunta

Sorry for my English..I have a code for two buttons to mute and unmute sounds..when I click to mute the sounds, it works but when I click to the other button to make the sounds to play it doesn't worked..any help?

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;
}
¿Fue útil?

Solución

I got your code working with a few small changes:

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;
}

Otros consejos

As Marcela says, try taking out the line "clicktoPlay =!clicktoPlay;" and see if it works. The way your code is now, until "soundfin" is called, clicktoPlay will remain true no matter how often speaker2sound is called.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top