A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How Do I stop Stop One Sound and Start Another? (Been stuck for hours)

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Location
    a
    Posts
    16

    Angry How Do I stop Stop One Sound and Start Another? (Been stuck for hours)

    So I have 20 buttons on a page..and i want it so that a certain button when i click it.. it starts a sound and cuts off all the other ones that may have been playing...this is my code...

    on (press) {
    stopAllSounds();
    }
    on (release) {
    mySound.attachSound("sound");
    mySound.start();
    }


    ..but it doesn't work

    what am i doing wrong?

    I made sure to enable action script in the linkage library too
    gabem

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    You forgot to make a new Sound Object


    on (release) {
    mySound = new Sound();
    mySound.attachSound("sound");
    mySound.start();
    }

    Hope this helps you
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Apr 2004
    Location
    a
    Posts
    16
    FINALLY!
    thank u so much!

    2ND QUESTION THOUGH

    If I wanted the original button that stopped to fade back in once the other one is done playing how would I do that?
    Last edited by gabemarchionda; 07-11-2011 at 09:43 AM.
    gabem

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Well, that's ALOT more complicated, and someone else, recently, wanted to achieve the same thing. I used some time writing a long code, so if you want it, you can have it - though, I can't explain it right now, because I have to do something, but here it is:

    Let's say you have 3 Buttons. On your first button, add this code:

    Code:
    on(press){
    	_root.btn(1, "sound1");
    }
    The number 1 is nothing you should be worried about, but the "sound1" is the sound's linkage name. So for the rest of the buttons, add this code on them too, but for each button, increase the number 1 - so, for example you have 3 buttons, on button 3, you'd have this code:

    Code:
    on(press){
    	_root.btn(3, "my_awesome_sound");
    }
    "my_awesome_sound" is the Linkage name of a sound in the Library, and see that I increased the number to 3, for button 2, I'd write number 2!

    But, for the real magic to happen, paste this code on your Frame:

    Actionscript Code:
    speed = 20; // change this number to change speed of fading
    checker = false;
    cur_sound = "";

    function btn(i, my_sound){
        this["sound"+i] = new Sound();
        this["sound"+i].attachSound(my_sound);
        _root.cur_sound = this["sound"+i];
        my_cur_sound = this["sound"+i];
        cur_volume = _root.cur_sound.getVolume();
        onEnterFrame = function(){
            vol = _root.cur_sound.getVolume();
            if(vol <= 0 || _root.checker == false){
                my_cur_sound.stop();
                this["sound"+i].start(0,1);
                delete onEnterFrame;
                _root.checker = true;
                myVolume = 0;
                onEnterFrame = function(){
                    if(myVolume <= 99){
                        myVolume += speed;
                        this["sound"+i].setVolume(myVolume);
                    } else {
                        this["sound"+i].setVolume(100);
                        delete onEnterFrame;
                    }
                }
            } else if(vol > 0){
                cur_volume -= speed;
                my_cur_sound.setVolume(cur_volume);
            }
        }      
    }

    I know, this code is huge, but I am proud that I wrote it by myself ^^

    But, for this code to work, you'll have to give your first sound, the linkage name of sound1, your second, sound2, sound3, etc.

    Now, check those buttons, and see if it is what you want. I'll be busy in 30 minutes now, so don't expect an answer until then if you have any further questions - hope this help you
    Last edited by Nig 13; 07-11-2011 at 09:54 AM.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Junior Member
    Join Date
    Apr 2004
    Location
    a
    Posts
    16
    ok i changed all the linkage names like you said to correspond properly...

    heres my question

    when you say "paste this code on your frame" where exactly do u mean? paste it in a new layer? cuz thats what i did

    and i ended up with this

    http://www.swfcabin.com/open/1310397225

    which isn't what i was hoping, is this what you thought i meant?


    What I was actually hoping for .....is that lets say i click button 1....and its playing then decide to click button 2 (while button 1 is still playing),,,i would want button 1 to then fade to a low volume for the duration of button 2's soundclip and then fade back to full volume once sound 2 has completed its sound


    Thanks so much for all your help so far
    Last edited by gabemarchionda; 07-11-2011 at 12:04 PM.
    gabem

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