5 Replies Latest reply on Apr 3, 2007 9:06 AM by Rothrock

    can this code be shorted? it's a small function

    chrisatflash Level 1
      Hello,

      Oke, here is what i am trying to do. I have attached multiple movieclips on the stages.
      Now i want when rolling over the movieclips gotoAndPlay(2).
      It works, but to type this to more then 30 clips.. isn't there a shorter way to do this?

      This is de code i use to use the rollover over and over again.

      this.tegels1.onRollOver = function() {
      tegels1.gotoAndPlay(2);
      }
      this.tegels2.onRollOver = function() {
      tegels2.gotoAndPlay(2);
      }
      this.tegels3.onRollOver = function() {
      tegels3.gotoAndPlay(2);
      }
      this.tegels4.onRollOver = function() {
      tegels4.gotoAndPlay(2);
      }
      this.tegels5.onRollOver = function() {
      tegels5.gotoAndPlay(2);
      }
      this.tegels6.onRollOver = function() {
      tegels6.gotoAndPlay(2);
      }
      this.tegels7.onRollOver = function() {
      tegels7.gotoAndPlay(2);
      }

      this.tegels8.onRollOver = function() {
      tegels8.gotoAndPlay(2);
      }

      ..i stoped typing.. i should write this to 30.

      Does someone has an idea for this to make it shorter?


        • 1. Re: can this code be shorted? it's a small function
          douggler
          Put your menu in a movie clip with an instance named 'menu'.
          On your actions layer, put this function:

          btnRollover = function (btnName) {
          menu[btnName].gotoAndPlay(2);
          };

          On your movieclips buttons, put this code:


          this.onRollover = function() {
          _root.btnRollover(_name);
          };

          This way you can add more functionality to your function in one place.

          Hope that helps
          • 2. Re: can this code be shorted? it's a small function
            Rothrock Level 5
            for(i=1;i==8;i++){
            var curClip:MovieClip=this["tegels"+i]
            curClip.ID=i;
            curClip.onRollOver=function(){
            this.gotoAndPlay(2);
            }
            }

            That is it. Make sure that all 8 clips exist when this code is executed. And if external content is loaded in afterwards then this will go away.

            I've also added an ID property to each clip that will make it easy to do specific actions based on interaction with those clips. Suppose each clip needs to tell a main clip to go to a specific farme, tegels1 goes to frame 1, tegels2 goes to frame to go to 2 and so on. Then you would add this to the for loop:

            curClip.onRelease=function(){
            mainClip.gotoAndStop(this.ID);
            }
            • 3. can this code be shorted? it's a small function
              chrisatflash Level 1
              Thanks douggler and Rothrock !

              I prefered the Rothrock method. I did not get your (douggler) code to work.
              I noticed a spelling error in this line: this.onRollover = function() {
              Rollover should be RollOver, but still i did not got it to work..
              I 'm curious what it could me as i want to learn more of flash.

              Also the ID function of Rothrock is a great way to to control movieclips.
              Thanks!

              • 4. Re: can this code be shorted? it's a small function
                jl2000 Level 1
                Hi - I was lurking around this thread and would also like a bit more of a "translation" of the Rothrock code. I also have questions regarding your comment :
                quote:

                TextThat is it. Make sure that all 8 clips exist when this code is executed. And if external content is loaded in afterwards then this will go away.


                What would prevent all clips from existing and why will this code go away if external content is loaded?

                Thanks for your help.
                JL
                • 5. Re: can this code be shorted? it's a small function
                  Rothrock Level 5
                  Michealnl – glad that is working out for you. Just a note, don't edit posts. A lot of the regulars here on the forum use newsreaders and they won't see them. So just make a new post.

                  jlucchesi – You would be surprised howmany folks make the "doesn't yet exist" mistake! Let's say you have a nice setup with some kind of loader/setup on frame 1 and then the actual application on frame 10. So they decide to set up all the rollovers on frame 1, but the actual clips aren't on the stage until frame 10. So from actionscripts perspective those clips don't exist.

                  As for why would the code go away if external content is loaded? Well, that is because that is the way it is! :) If you wanted to keep the code and also load content a good way would be to create an empty movie clip inside of each of the tegels (or whatever) clip. I usually call those clips on the inside as "art" or "background" or some such. I can then apply the mouse event codes to the outer tegels and load/swap external files into the art file.