8 Replies Latest reply on Nov 5, 2010 10:22 AM by kglad

    unified Rollover function on multiple MC's

    ChrisH

      Hi all,

      I'm trying to code a variable rollover for a sequence of MCs on the stage.

      I have 3 buttons (MovieClips) on the stage and each time one is rolled over I'm making a different movieclip appear.

       

      So I've done this in a root timeline frame script:

       

      //put an empty container movieclip on the stage

      containerMC = _root.createEmptyMovieClip("container", this.getNextHighestDepth());

       

      //button 1  - "but1" is my linked instance name and Content 1 is my content movieclip

       

      but1.onRollOver = function() {
      trace("You rolled over on the movie clip '"+this._name+"'.");
      newContent=_root.attachMovie("Content1","container",containerMC.getDepth());
      newContent._x=300;
      newContent._y=200;

      };

       

      and so on for each button (1, 2 and 3).

      As you can imagine if there were 20 buttons there is a lot of uneccessary duplication.

       

      What I've considered is, as they are sequential I should be able to read "but"+i and "content"+i and I could push the button names and content names into an array.  The thing I'm having trouble with is, how do I write the function so it checks which clip is rolled over and then attaches the corresponding content movieclip?

       

      Ultimately I would like the script to check how many buttons there are on the stage and simply iterate for that number (so I don't have to specify how many there are to loop through i.e. numOfButtons = 4),  But not sure how to go about it.

       

      As you can see the code could be the same for each button, if I could read the movieclip rolled over and attach the function variably.

       

      In summary what I'm trying to do is have a single function for all rollover buttons named but1 to but n.

       

      Any guidance would be appreciated.

      Chris

        • 1. Re: unified Rollover function on multiple MC's
          kglad Adobe Community Professional & MVP

          unless you can make some assumptions (that are probably unwise), you need to specify the number of movieclip buttons:

           


           

          //put an empty container movieclip on the stage

          containerMC = _root.createEmptyMovieClip("container", this.getNextHighestDepth());

           

          for(var i:Number=1;i<=buttonNumber;i++){  // define buttonNumber

          this["but"+i].ivar=i;

          this["but"+i].onRollOver=function(){

          removeF();

          var mc:MovieClip=containerMC.attachMovie("Content"+this.ivar,"content"+this.ivar,containerMC. getNextHighestDepth());

          mc._x=300;

          mc._y=200;

          }

          }

          function removeF(){

          for(obj in containerMC){

          if(typeof(containerMC[obj])=="movieclip"){

          containerMC.removeMovieClip(containerMC[obj]);

          }

          }

          }

           


          1 person found this helpful
          • 2. Re: unified Rollover function on multiple MC's
            ChrisH Level 1

            Thanks Kglad,

            I've inserted the script and set buttonNumber to 3 prior to entering into the for loop.

            Unfortunately it only works twice and then halts - irrespective of the number of buttonNumber.

            Would I have to wrap it in an enterFrame.

             

            So there's no way to see how many movieclips are on the stage at any one time called "but"+i?

            If there was I could use that to set the iteration, but wouldn't have a clue how to do it.

             

            Thanks for your help, it's certianly well further along that I was.

            Chris

            • 3. Re: unified Rollover function on multiple MC's
              ChrisH Level 1

              I've tried things a bit differently to see if it would work but still can't seem to get it working.

               

              I've created a function in a frame script on the main timeline where each of my instances are sitting:

               

              //

              function rollover(){
                    this[_root.rolledOver].onRollOver=function(){
                    trace("You rolled over on the movie clip '"+rolledOver+"'.");
                    newContent=_root.attachMovie("Content"+r,"container",containerMC.getDepth());
                    newContent._x=300;
                    newContent._y=200;
                   }
              }

              //

               

              and on each of my instance have written similar to:

               

              //

              on(rollOver){
                    _root.r = 1;
                    _root.rolledOver=this._name;
                    _root.rollover.call();
              }

              //

               

               

              The function is being called ok but it seems that the line

                this[_root.rolledOver].onRollOver=function(){

              or more specifically my variable instance reference is causing the issue, because I can change this to but1 and it works fine.

              Any suggestions?

               

              Regards

              Chris

              • 4. Re: unified Rollover function on multiple MC's
                kglad Adobe Community Professional & MVP

                copy and paste the code i suggested onto the timeline that contains your buttons.  make sure you have all your linkage id's defined.

                 

                if you still have trouble after doing that, use the trace() function to debug.

                • 5. Re: unified Rollover function on multiple MC's
                  ChrisH Level 1

                  Hi kglad,

                  Ok, have tried it again and double checked all linkages but still only works no more than twice.  Will use trace and see what I can deduce.

                  Thankyou again or your help.

                  Regards

                  Chris

                  • 6. Re: unified Rollover function on multiple MC's
                    kglad Adobe Community Professional & MVP

                    i have an error in removeF().  that should be:

                     

                    function removeF() {
                        for (obj in containerMC) {
                            if (typeof (containerMC[obj]) == "movieclip") {
                                containerMC[obj].removeMovieClip();
                            }
                        }
                    }

                    • 7. Re: unified Rollover function on multiple MC's
                      ChrisH Level 1

                      Hi kglad,

                      works great, thanks so much.

                       

                      I still haven't worked out why your:

                       

                      this["but"+i].onRollOver = function()

                      worked, but the:

                      this[_root.rolledOver].onRollOver=function()

                       

                      one I wrote didn't.  I'll see what I can learn out of it.

                       

                      You obviously put a fair effort into helping people on the forum, I'm sure it's appreciated.

                       

                      Best Regards

                      Chris.

                      • 8. Re: unified Rollover function on multiple MC's
                        kglad Adobe Community Professional & MVP

                        you're welcome.

                         

                        the array operator [ ] can be used to help flash resolve strings into objects.  so, if you have an object with instance name but1 on the current timeline, you can use:

                         

                        this["but1"]

                         

                        to reference the object and you can therefore apply a mouselistener to that object:

                         

                        this["but1"].onRelease=function(){

                        }

                         

                        of course, there's not too many occasions when you would do that because you can more directly use:

                         

                        but1.onRelease=function(){

                        }

                         

                        however,  what i used:

                         

                        for(var i:Number=1;i<=whatever;i++){

                        this["but"+i].onRelease=function(){

                        }

                        }

                         

                        is much more useful.