A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Sound On/Off Button in Movieclip HELP!!

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2

    Question Sound On/Off Button in Movieclip HELP!!

    Hey everyone I've got a real simple AS2 question for you pros!

    I'm trying to make a button that when clicked executes a sound clip and plays all the way through. Also, if the button is clicked while the sound is still playing the sound needs to stop. I have no problem getting this to work using Adobe's tutorial.

    They have me first place this code in the first frame:
    my_sound = new Sound();
    my_sound.attachSound("bubble_01sound");
    play();

    Next I create two buttons, an on and an off which are placed separately in the second and third frames respectively. Code is applied directly to the buttons.

    The on button:
    on (release) {
    _root.my_sound.start(0,1000);
    _root.gotoAndStop("stop");
    }

    The off button:
    on (release) {
    _root.my_sound.stop();
    _root.gotoAndStop("play");
    }

    Finally, I name the 2nd frame "play" and 3rd frame "stop" and add stop(); to both frames. Oh I also set up the linkage so that the sound's identifier is "bubble_01sound".

    This is all well and good however, here's the rub. Unlike their example which takes place in the main timeline, my button needs to bob up and down. So I put everything from the tutorial in a movieclip and on the main timeline animate the movieclip as I like. When this plays the button does not play sound. I took this as the initial sound code not being activated because its in the movieclip. If I take the sound code out of the movieclip and place it in the first frame of the main timeline, the button does play sound but the stop function does not work. I can keep clicking the button which repeats the sound on top of the already playing sound.

    So it seems to me there are three options. One, I need to find a way for the sound code to be activated when its seated in the movieclip. Or two, with the sound code in the main timeline, I need a way for the on/off buttons to reference the code which is one level up in the main timeline. Or the third option would be that I'm completely wrong... I'd put my money on the third option.

    So if anyone can figure out how I can have this button work properly while seated in a movieclip, I would greatly appreciate it, thanks!

  2. #2
    Senior Member
    Join Date
    May 2008
    Posts
    332
    I think you might be better off by placing the button actions on the main time, with proper instance name, (which may now be inside another movie clip). Then you can "drill down" into that movie clip using dot notation, something like
    my_movie_clip.sound_on.onRelease = function() {
    you function goes here
    }
    So in my_movie_clip go to the sound_on and do the onRelease
    Best wishes,
    EfV

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    EfV, thanks so much for the advice, unfortunately my ignorance precedes me. I believe I've followed you correctly but maybe you or someone can see my error. As is, everything works except for all parts of the off button. Sound plays and the button switches to the off button. But the off button does not stop the sound nor does it bring me back to the on button again.

    Main timeline is only 1 frame long. A keyframe with the movie clip called bubble01 and an empty frame with this code:

    my_sound = new Sound();
    my_sound.attachSound("bubble_01sound");
    play();

    bubble01.bubble01on.onRelease = function() {
    _root.my_sound.start(0,1000);
    bubble01.gotoAndStop("stop");
    }

    bubble01.bubble01off.onRelease = function() {
    _root.my_sound.stop();
    bubble01.gotoAndPlay("play");
    }

    Finally, the movie clip is 2 frames long and each frame has a stop(); on it. 1st frame titled "play" has bubble01on button and 2nd frame titled "stop" has bubble01off button on it.

    Continued help is much appreciated!
    thanks

  4. #4
    Senior Member
    Join Date
    May 2008
    Posts
    332
    I'm just not exactly sure how you want the bobbing buttons to work but here is some code that will get the sound working correctly.
    It's a little different approach but hopefully you can adapt at least parts of it.
    First I created the 2 buttons as movie clips. In this case they are 20 frames long with a bouncing action. Gave them the same names you used. No actions or sound attached to either, just the bouncing. These where created in the library and not yet on the stage.
    The main timeline is just one frame. Drag the 2 movie clip buttons onto the stage and give each the instance names you used, bubble01on and bubble01off
    Then in actions layer attached the sound but turned it off so it's not playing all the time
    Code:
    my_sound.stop();
    then added the actions for the buttons, but added the stop sound to the "on" button to stop any previously playing sounds.
    Code:
    var my_sound = new Sound();
    my_sound.attachSound("bubble_01sound");
    my_sound.stop();
    
    bubble01on.onRelease = function() {
    	_root.my_sound.stop();
    	_root.my_sound.start(0,1000);
    }
    bubble01off.onRelease = function() {
    _root.my_sound.stop();
    }
    This solution seems to work just fine when I tested it. No sound until the bouncing "on" is pressed. Press "on" again and the sound starts over, no overlap. "off" stops the sound.
    Best wishes,
    EfV

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center