7 Replies Latest reply on May 3, 2006 6:15 PM by David_Kelly

    fledgling AS coder needs help: controlling mc's from btn's

    gdhammond Level 1
      I am a novice AS coder. I have a project in which I have multiple buttons, each of which triggers a movie clip to play. Each movie clip is just an image which fades in and then fades out. I have put the labels "in" and "out" in each movie clip, and would like to point to these from each corresponding button.

      The problem I'm having is trying to figure out how to play a movie clip's "out" fading sequence when a different button is pressed (at the same time, playing the "in" fading sequence of its own mc). I'm sure this is an obvious procedure to all of you who live and breathe AS, but I'm just not there yet.

      Thanks to anyone who can provide some quick insight on this.
        • 1. Re: fledgling AS coder needs help: controlling mc's from btn's
          Level 7
          gdhammond,

          > I am a novice AS coder.

          Howdy.

          > The problem I'm having is trying to figure out how to
          > play a movie clip's "out" fading sequence when a different
          > button is pressed (at the same time, playing the "in" fading
          > sequence of its own mc).

          I think I can visualize what you're saying, but it would help if you
          could show your code so far.

          > I'm sure this is an obvious procedure to all of you who live
          > and breathe AS, but I'm just not there yet.

          Yeah, let's see your code, to see what you've done so far. There are at
          least two ways to code up a button, so I'd like to see which approach you
          took.


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: fledgling AS coder needs help: controlling mc's from btn's
            gdhammond Level 1
            Hi David,

            Listed below is my code so far ... it's incomplete, simply because I don't know how to handle coding the buttons and movie clips so that they interact properly.

            I should mention that a named instance of each movie clip is just sitting on the stage (stopped at the first frame, at which the mc is invisible ... Alpha=0 ). My intent was to have each button play the fade in sequence ("in" - a tween starting at frame 2), and then when another button is pressed, play the fade out sequence ("out" - a tween starting at frame 11) for that movie clip.

            Does each movie clip have to "listen" somehow for other buttons to be pressed, and then "gotoAndPlay" its own "out" tween? Or perhaps some sort of "if mc is at frame 11, then gotoAndPlay "out" when any button is pressed"??

            Thanks for your help with this, David.

            My sample button code:
            • 3. Re: fledgling AS coder needs help: controlling mc's from btn's
              Level 7
              gdhammond,

              > I should mention that a named instance of each movie
              > clip is just sitting on the stage (stopped at the first frame,
              > at which the mc is invisible ... Alpha=0).

              Okay.

              By "on the stage," I assume you main "in the main timeline" (as opposed
              to the timeline of a particular movie clip).

              > My intent was to have each button play the fade in sequence
              > ("in" - a tween starting at frame 2), and then when another
              > button is pressed, play the fade out sequence ("out" - a tween
              > starting at frame 11) for that movie clip.

              Is there a matched paring of one button for each clip? Button A
              instructs clip A; button B instructs clip B, and so on?

              > Does each movie clip have to "listen" somehow for other
              > buttons to be pressed,

              The amount of "listening" depends on how many buttons and clips there
              are, and what is expected of each clip. Suppose I click button A, which
              sends clip A to its "in" tween. Now I click button B. Are you saying clip
              A needs to be send to its "out" tween, while clip B is sent to its "in"
              tween? If so, it can get complicated, because each buttons either needs
              know which clip was manipulated last (if any) and tell it to go to "out," or
              it needs to tell *all* clips to go to "out" and figure out some other way to
              only accept the clip (or clips?) that need to.

              > perhaps some sort of "if mc is at frame 11, then gotoAndPlay
              > "out" when any button is pressed"??

              That may work. But again, how do you know which mc to ask, "Hey, are
              you at frame 11"?

              I could see setting a global variable in frame 1 of the main timeline
              that stores a reference to the last manipulated clip. Start it out as null,
              because no clip has yet been referenced.

              var lastClip:MovieClip = null;

              Next, code up your buttons like this:

              A) Check if lastClip is null. If it is, skip to point B in this list.
              If it isn't, send the movie clip referenced by lastClip to its "out" frame.

              B) Set lastClip to the pertinent movie clip that "belongs" to this
              button.

              C) Send the pertinent movie clip to its "in" frame.

              Actually, lets take the concept of point A and flip it around. If
              lastClip is *not* null, send the clip referenced by that variable to its
              "out" frame.

              on (release) {
              if (lastCip != null) {
              lastClip.gotoAndPlay("out");
              }
              lastClip = sample_mc_1;
              sample_mc_1.gotoAndPlay("in");
              }

              Does that make sense?


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: fledgling AS coder needs help: controlling mc's from btn's
                pwyon Level 1
                You could try fading in and out with script instead, like this:

                function fade( clip:MovieClip , newAlpha:Number , speed:Number ){
                this.onEnterFrame = function( ){
                clip._alpha -= ( clip._alpha - newAlpha ) / speed;
                }//end onEnterFrame
                }//end function

                //Use the function like this
                myClip.onPress = function( ){ //fade out to 50
                fade( myClip , 50 , 20 );
                }

                Play around with the "50" value to ease to a new alpha.
                Play around with the "20" value to change the speed.

                Hope this might help. Take care!
                • 5. Re: fledgling AS coder needs help: controlling mc's from btn's
                  David_Kelly
                  I'm in the same boat here. I want to use alpha to give the illusion of objects/clips leaving the stage. I just wanted to clarify some things:
                  1- does this script go in the button, on the main timeline, or in the clip.

                  2- Could you please the label Movie Clip name vs. the variable with a bold or italic. I'm a novice at this and I'm kind of dumb too.

                  function fade( clip:MovieClip , newAlpha:Number , speed:Number ){
                  this.onEnterFrame = function( ){
                  clip._alpha -= ( clip._alpha - newAlpha ) / speed;
                  }
                  }

                  circle.onPress = function() {
                  fade( myClip , 50 , 20 );
                  };
                  • 6. Re: fledgling AS coder needs help: controlling mc's from btn's
                    pwyon Level 1
                    You're not dumb. It was my fault. I should have been more clear. Sorry about that:

                    The "code" goes on the main timeline. Preferably on a layer all by itself called, "Actionscript". This way you can keep things clean.

                    "myClip" refers to the clip on the stage.

                    In your case it would be "circle", like this:

                    circle.onPress = function() {
                    fade( this , 50 , 20 ); // "this" refers to "circle".
                    };

                    you could even do this, for fun:

                    circle.onPress = function() {
                    fade( this , 50 , 20 );
                    };
                    circle.onRollOut = function() {
                    fade( this , 100 , 20 );
                    };

                    In my other example I used "myClip" instead of "this". Not good to do that, but it still works.
                    Sorry about that.


                    The code:
                    • 7. Re: fledgling AS coder needs help: controlling mc's from btn's
                      David_Kelly Level 1
                      Thanks for being so gracious. Worked like a charm.