6 Replies Latest reply on May 30, 2007 6:26 AM by Newsgroup_User

    shorten this code?

    chrisatflash Level 1
      Hello,

      I am trying to make this work ..

      I have this code:

      ball.onRollOver = function() {
      movie_mc.gotoandStop("ball");
      ball.gotoAndPlay(2);
      }

      red.onRollOver = function() {
      movie_mc.gotoAndStop("red");
      red.gotoAndPlay(2);
      }

      and so on.. like 20 times..

      So i thought maybe this code can be shorten this way:

      function shortcode(test) {
      movie_mc.gotoAndStop(test);
      test.gotoAndPlay(2);
      }

      ball.onRollOver = shortcode(ball);

      but this does not seem to work..

      Does anyone see what goes wrong?

      Regards,

      Micheal.
        • 1. Re: shorten this code
          Level 7
          Michealnl wrote:
          > Hello,

          > function shortcode(test) {
          > movie_mc.gotoAndStop(test);
          > test.gotoAndPlay(2);

          This does not work. You would have to reference test as array element.
          Like this:

          this._parent[test].gotoAndPlay(2);


          But ...


          > ball.onRollOver = shortcode(ball);

          This doesn't work too. You can't put function parameter on an event. If
          ball is a movieclip you can use the clipname and try this:

          this._parent[this._name].gotoAndPlay(2);

          But easier than this you could just try this ..

          this.gotoAndPlay(2);


          .. instead of referencing to itself by name.
          • 2. Re: shorten this code?
            chrisatflash Level 1
            so it's not possiblee to try a function parameter on an event?
            Is there another way then to apply more actions like in my example
            function on a onRollOver event and to pass a parameter so
            i don't have to type every function repeatedly?
            • 3. Re: shorten this code?
              nITiNkIlLeRmEeRuT Level 1
              try this
              • 4. Re: shorten this code
                Level 7
                You're thinking is close to what you want, but you need to do something like
                so:

                var shortcode = function(){
                movie_mc.gotoAndStop(this._name);
                this.gotoAndPlay(2);
                }

                ball.onRollOver = shortcode;
                red.onRollOver = shortcode;

                You have to assign a function to the event handlers. However, as you have 20
                of them I'd probably do it with an array, as the prior responder said.

                --
                Dave -
                Head Developer
                http://www.blurredistinction.com
                Adobe Community Expert
                http://www.adobe.com/communities/experts/


                • 5. Re: shorten this code?
                  Level 7
                  Michealnl wrote:
                  > so it's not possiblee to try a function parameter on an event?

                  no.

                  > Is there another way then to apply more actions like in my example
                  > function on a onRollOver event and to pass a parameter so
                  > i don't have to type every function repeatedly?

                  Another way or workaround when you want to pass a parameter to an event
                  it to put a varaible set a variable on the clip. So if you have a loop
                  where you set the event-script of a clip you would do it like this.
                  Let's say you have 20 Balls (named "ball1_mc", "ball2_mc" ..)


                  fx = functon(){
                  // "this" would be the movieclip you put the script on
                  trace("this is Ball No. " + this.myID)
                  }

                  for (n=1;n<=20;n++){
                  _level0["ball" + n + "_mc"].myID = n;
                  _level0["ball" + n + "_mc"].onRollOver = fx;
                  }


                  But this won't work for buttons. Because buttons can't cary a value,
                  afaik. You would have to put a button in a clip to work with buttons.
                  • 6. Re: shorten this code?
                    chrisatflash Level 1
                    Thanks guys! This works great! It saves me a lot of typing in actionscript!

                    Regards

                    Micheal.