• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

AS3 Audio Toggle Hiccup

New Here ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

I call it a hiccup because the code works; but it's backwards. When I hit the audio button it plays the music and when I hit it again it stops the music.

I wish for the music to play automatically when you enter the SWF and then you hit the button to mute the music if you wish; and then hit the button again to play the music again.

Here is the code I have:

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);

toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);

toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);

toggleButton.buttonState = "On";

var song:Sound = new TitleMusic();

var songChannel:SoundChannel;

function rolloverToggle(event:MouseEvent){

    toggleButton.gotoAndStop(toggleButton.buttonState+" over");

}

function rolloutToggle(event:MouseEvent){

    toggleButton.gotoAndStop(toggleButton.buttonState);

}

function toggleClick(event:MouseEvent){

    if (toggleButton.buttonState == "on"){

        toggleButton.buttonState = "off";

        stopSong();

    }else{

        toggleButton.buttonState = "on";

        startSong();

    }

    toggleButton.gotoAndStop(toggleButton.buttonState+ "over");

}

function startSong(){

    songChannel = song.play();

    songChannel.addEventListener(Event.SOUND_COMPLETE, songCompleteHandler)

}

function stopSong(){

    songChannel.stop();

}

function songCompleteHandler(event:Event){

    toggleButton.buttonState = "off";

    toggleButton.gotoAndStop(toggleButton.buttonState+" over");

}

I tried experimenting but I'm just making it worse. Any advice?

TOPICS
ActionScript

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 30, 2011 Dec 30, 2011

well, look at the frame labeled "on".  is that what you want displayed with the sound is playing?

Votes

Translate

Translate
Community Expert ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

assuming buttonState="on" is what you want when the sound is playing, use:

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);

toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);

toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);

toggleButton.buttonState = "on";

var song:Sound = new TitleMusic();

var songChannel:SoundChannel=song.play();

function rolloverToggle(event:MouseEvent){

    toggleButton.gotoAndStop(toggleButton.buttonState+" over");

}

function rolloutToggle(event:MouseEvent){

    toggleButton.gotoAndStop(toggleButton.buttonState);

}

function toggleClick(event:MouseEvent){

    if (toggleButton.buttonState == "on"){

        toggleButton.buttonState = "off";

        stopSong();

    }else{

        toggleButton.buttonState = "on";

        startSong();

    }

    toggleButton.gotoAndStop(toggleButton.buttonState+ "over");

}

function startSong(){

    songChannel = song.play();

    songChannel.addEventListener(Event.SOUND_COMPLETE, songCompleteHandler)

}

function stopSong(){

    songChannel.stop();

}

function songCompleteHandler(event:Event){

    toggleButton.buttonState = "off";

    toggleButton.gotoAndStop(toggleButton.buttonState+" over");

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

Well now the sound toggling works but the icons don't.

The music is plays automatically which is great and when I hit the button again it turns off. But the speakerphone doesn't match. Just by moving my mouse over the speakerphone it crosses it out and then when I hit it the speakerphone will be normal but the music will stop.

And then it's pretty much stuck in that pattern. (In other words the icon isn't matching the sound.)

It is great that the sound works now though.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

buttonState="on" is what you want when the sound is playing?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

I was going to try and attach the file, but I guess there's no option for that.

Since I'm not entirely sure (as I followed a tutorial) here's everything that's labeled:

- toggleButton is the instance name of the movie clip

When you double click on the movie clip there are 4 keyframes:

1st one named "off", second one named "off over", 3rd one named "on", and 4th one named "on over" (First 2 frames are a speakerphone and the last 2 are crossed out speakerphones. And only the first frame has actionscript in it; which is stop)

TitleMusic is the sound

I'm a little disappointed that there is no option to just attach the flash file. But I do appreciate your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

well, look at the frame labeled "on".  is that what you want displayed with the sound is playing?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 31, 2011 Dec 31, 2011

Copy link to clipboard

Copied

Thank you! I got it working now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 31, 2011 Dec 31, 2011

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines