7 Replies Latest reply on Jan 24, 2007 3:20 PM by Newsgroup_User

    fade in - fade out bg loop

    gdmt
      hi everyone!
      i'm making a one on-off button to play-stop a bg loop.

      this is the timeline AS:

      setVolumen = function (soundObj, startLevel, maxLevel, inc) {
      soundObj.setVolume(startLevel);
      this.createEmptyMovieClip("loopSound", this.getNextHighestDepth());
      startLevel<maxLevel ? vol=inc : vol=-inc;
      this.loopSound.onEnterFrame = function() {
      startLevel += vol;
      soundObj.setVolume(startLevel);
      if (soundObj.getVolume() == maxLevel) {
      delete loopSound.onEnterFrame;
      }
      };
      };
      musica_ambiente = new Sound(this);
      musica_ambiente.attachSound("ambience");

      musica_ambiente.setVolume(0);
      musica_ambiente.start(0, 9999);
      setVolumen(musica_ambiente, 0, 50, 0.5);


      and this are the AS to control it:
      off:
      setVolumen(musica_ambiente, musica_ambiente.getVolume(), 0, 0.5);
      on:
      setVolumen(musica_ambiente, musica_ambiente.getVolume(), 10, 0.5);

      i don't know, i made 2 buttons and put'em inside a movieclip (one in the first frame, and the other in the second) and added'em the previous AS with gotoAndStop(1); and gotoAndStop(2); but it doesn't work...

      can someone help me to make it work?

      thanx a lot in advance!

      cheers!
        • 1. Re: fade in - fade out bg loop
          Level 7

          > and this are the AS to control it:
          > off:
          > setVolumen(musica_ambiente, musica_ambiente.getVolume(), 0, 0.5);
          > on:
          > setVolumen(musica_ambiente, musica_ambiente.getVolume(), 10, 0.5);
          >
          > i don't know, i made 2 buttons and put'em inside a movieclip (one in the first
          > frame, and the other in the second) and added'em the previous AS with
          > gotoAndStop(1); and gotoAndStop(2); but it doesn't work...
          >
          > can someone help me to make it work?
          >
          > thanx a lot in advance!

          Once you in a movie clip, you no longer refer to the main timeline but the
          movie clip timeline. You need to _parent your function so it can find its
          way out of the movie clip. _parent will bring it one level up.



          --
          Best Regards

          Urami


          --

          !!!!!!! Merry Christmas !!!!!!!
          &
          Happy New Year


          <urami>
          If you want to mail me - DO NOT LAUGH AT MY ADDRESS
          </urami>
          • 2. Re: fade in - fade out bg loop
            gdmt Level 1
            thanx 4 reply!
            yeah, i know urami_...
            i tried absolute and relative target paths...
            but... nothing...
            :'(
            • 3. Re: fade in - fade out bg loop
              gdmt Level 1
              :S
              i'm triying to make it work but i can't...

              can someone check what's wrong?
              http://www.backupmyfiles.net/download.php?file=513491

              i'll really apreciate it...
              • 4. Re: fade in - fade out bg loop
                Level 7


                gdmt wrote:
                > thanx 4 reply!
                > yeah, i know urami_...
                > i tried absolute and relative target paths...
                > but... nothing...

                Because the function sits on the _parent, and the musica_ambiente object sits on the _parent,
                you will need to use multiple _parent prefix in the path.

                _parent.setVolumen(_parent.musica_ambiente, _parent.musica_ambiente.getVolume(), 100, 0.5);

                However, you could create a function on the parent that makes the entire call:
                function fadeOutAmbiente() { setVolumen(musica_ambiente, musica_ambiente.getVolume(), 0, 0.5); }
                and then you'd just call that from the movieclip: _parent.fadeOutAmbiente();

                If you have several different sound objects on the same scope that you want to fade out from
                different scopes, you could go further and make a function like this:

                function fadeOut(soundName) { setVolumen(this[soundName], this[soundName].getVolume(), 0, 0.5); }
                and then you'd call it: _parent.fadeOut("musica_ambiente");

                Anyhow, I did some changes to your original code to prevent the multiple _parent references:




                setVolumen = function (soundName, maxLevel, inc) {
                var soundObj=this[soundName],
                startLevel=soundObj.getVolume();
                if (startLevel==maxLevel) return; // Abort if no change needed
                inc=Math.abs(inc)*(startLevel<maxLevel ? 1 : -1); // Enforce correct sign
                if (this.loopSound) this.loopSound.removeMovieClip(); // Kill old volume tween

                var mc=this.createEmptyMovieClip("loopSound", this.getNextHighestDepth()); // Create temp movieclip
                mc.onEnterFrame = function() {
                soundObj.setVolume(startLevel += inc);
                if (soundObj.getVolume() == maxLevel) {
                delete loopSound.onEnterFrame;
                this.removeMovieClip(); // Kill temp movieclip
                }
                };
                };
                musica_ambiente = new Sound(this);
                musica_ambiente.attachSound("ambience");
                musica_ambiente.setVolume(0);
                musica_ambiente.start(0, 9999);
                setVolumen("musica_ambiente", 100, 0.5);


                ///////////////

                Fade Out

                on (release) {
                _parent.setVolumen("musica_ambiente", 0, 0.5);
                }


                Fade In
                on (release) {
                _parent.setVolumen("musica_ambiente", 100, 0.5);
                }

                NOTE !!!

                It should now also fade nicely even if you click while another fade is in progress
                because it kill off the old one before creating the new.


                --
                Best Regards

                Urami


                --

                !!!!!!! Merry Christmas !!!!!!!
                &
                Happy New Year


                <urami>
                If you want to mail me - DO NOT LAUGH AT MY ADDRESS
                </urami>
                • 5. Re: fade in - fade out bg loop
                  gdmt Level 1
                  oh! thank you very much urami_ !!!
                  i'll check it!
                  :D
                  • 6. Re: fade in - fade out bg loop
                    gdmt Level 1
                    it worked perfectly!
                    thanx a lot urami_!!!

                    THANK YOU!
                    • 7. Re: fade in - fade out bg loop
                      Level 7


                      gdmt wrote:
                      > it worked perfectly!
                      > thanx a lot urami_!!!
                      >
                      > THANK YOU!

                      You most Welcome :)
                      Sorry I did not reply earlier, I spent most of my time
                      on flash not flash.actionscript forum. Did not notice
                      the follow up.
                      Anyway, glad you got it working.

                      You have a nice day gdmt :)

                      --
                      Best Regards

                      Urami


                      --

                      !!!!!!! Merry Christmas !!!!!!!
                      &
                      Happy New Year


                      <urami>
                      If you want to mail me - DO NOT LAUGH AT MY ADDRESS
                      </urami>