10 Replies Latest reply on Aug 11, 2006 5:08 PM by blemmo

    How can I reduce this code?

    Josh_Pratt
      I'm trying to work on reducing some of my actionscript code, to get better and more efficient in writing it. I'm at a loss as to how to reduce this code. I've tried a number of things but it never works.

      This is on the first frame of the actions layer within the movieclip that all the buttons are located. The buttton names are "menuButton0", "menuButton1" .... through "menuButton5". Here is the code:

      function enableButtons(){
      menuButton0.enabled = true;
      menuButton1.enabled = true;
      menuButton2.enabled = true;
      menuButton3.enabled = true;
      menuButton4.enabled = true;
      menuButton5.enabled = true;
      menuButton0._alpha = 100;
      menuButton1._alpha = 100;
      menuButton2._alpha = 100;
      menuButton3._alpha = 100;
      menuButton4._alpha = 100;
      menuButton5._alpha = 100;
      }

      menuButton0.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(-106, .5, "easeOutElastic");
      menuButton0._alpha = 60;
      menuButton0.enabled = false;
      }
      menuButton1.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(-68, .5, "easeOutElastic");
      menuButton1._alpha = 60;
      menuButton1.enabled = false;
      }
      menuButton2.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(-28, .5, "easeOutElastic");
      menuButton2._alpha = 60;
      menuButton2.enabled = false;
      }
      menuButton3.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(10, .5, "easeOutElastic");
      menuButton3._alpha = 60;
      menuButton3.enabled = false;
      }
      menuButton4.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(49, .5, "easeOutElastic");
      menuButton4._alpha = 60;
      menuButton4.enabled = false;
      }
      menuButton5.onRelease = function() {
      enableButtons();
      menu_bar.ySlideTo(88, .5, "easeOutElastic");
      menuButton5._alpha = 60;
      menuButton5.enabled = false;
      }
        • 1. Re: How can I reduce this code?
          everynewday
          You could change the first part to the code I attached. The rest of it, however, would be a little tricky to condense, since you're not moving the others to a consistent space apart. There may be a way, but I'm not sure how.
          • 2. Re: How can I reduce this code?
            Level 7
            The first thing you can do is use bracket notation to reference your
            clips. For instance, the enableButtons() function could be rewritten as:

            function enableButtons(){
            for(var i = 0; i < 6; i++){
            this["menuButton" + i].enabled = true;
            this["menuButton" + i]._alpha = 100;
            }
            }

            You can do something similar with your onRelease functions, but since
            you have different values for each button you could use an array to
            store their positions like so:

            var btnArray:Array = [-106, -68, -28, 49 88];

            and then make one generic addMouseEvents function:

            function addMouseEvents () {
            for (var i = 0; i < 6; i++) {
            this["menuButton" + i].onRelease = function () {
            enableButtons ();
            menu_bar.ySlideTo (btnArray , .5, easeOutElastic");
            this._alpha = 60;
            this.enabled = false;
            };
            }
            }


            And simply call addMouseEvents() to get the buttons working:

            addMouseEvents();



            HTH

            Dave
            • 3. Re: How can I reduce this code?
              Josh_Pratt Level 1
              I added those lines of code... But only the enable and alpha parts are working.

              I'm really not quite sure hot to use arrays.. I'm assuming its an array problem. Here's my code in all: What should I fix?

              stop();

              #include "mc_tween2.as"

              var btnArray:Array = [-106, -68, -28, 49, 88];

              for(var i = 0;i <= 5; i++){
              this["menuButton"+i].onRollOver=function(){
              this._alpha=60;
              }
              this["menuButton"+i].onRollOut=function(){
              this._alpha=100;
              }
              }

              function enableButtons() {
              for (var i = 0; i <= 5; i++) {
              this["menuButton"+i].enabled = true;
              this["menuButton"+i]._alpha = 100;
              }
              }

              function addMouseEvents () {
              for (var i = 0; i < 6; i++) {
              this["menuButton" + i].onRelease = function () {
              enableButtons ();
              menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");
              this._alpha = 60;
              this.enabled = false;
              }
              }
              }

              addMouseEvents();
              • 4. How can I reduce this code?
                everynewday Level 1
                I believe the following line:

                menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");

                should read:
                • 5. Re: How can I reduce this code?
                  Josh_Pratt Level 1
                  I tried that already... The bar doesn't move at all still.
                  • 6. Re: How can I reduce this code?
                    Level 7
                    Oh, I think I see the problem. Because of the way the onRelease is
                    constructed I think you have improper scope to menu_bar.

                    Try changing this line:

                    menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");

                    to this:

                    _parent.menu_bar.ySlideTo (btnArray , .5, "easeOutElastic");



                    Dave
                    • 7. Re: How can I reduce this code?
                      Level 7
                      Also, you can stick the rollOver and rollOut functions into the
                      addMouseEvents function:

                      function addMouseEvents () {
                      for (var i = 0; i < 6; i++) {
                      this["menuButton" + i].onRelease = function () {
                      enableButtons ();
                      menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");
                      this._alpha = 60;
                      this.enabled = false;
                      }

                      this["menuButton"+i].onRollOver=function(){
                      this._alpha=60;
                      }

                      this["menuButton"+i].onRollOut=function(){
                      this._alpha=100;
                      }

                      }
                      }
                      • 8. Re: How can I reduce this code?
                        Josh_Pratt Level 1
                        It is still not working... Everything seems to work fine (rollovers, onreleases, enabled, alpha) except the bar moving. It is just frozen in place. Here is my code now:


                        stop();

                        #include "mc_tween2.as"

                        var btnArray:Array = new Array[-106,-68,-28,49,88];

                        addMouseEvents();

                        function addMouseEvents () {
                        for (var i = 0; i < 6; i++) {
                        this["menuButton" + i].onRelease = function () {
                        enableButtons ();
                        menu_bar.ySlideTo (btnArray , .5, "easeOutElastic");
                        this._alpha = 60;
                        this.enabled = false;
                        }
                        this["menuButton"+i].onRollOver=function(){
                        this._alpha=60;
                        }
                        this["menuButton"+i].onRollOut=function(){
                        this._alpha=100;
                        }
                        }
                        }

                        function enableButtons() {
                        for (var e=0;e<6;e++) {
                        this["menuButton"+e].enabled = true;
                        this["menuButton"+e]._alpha = 100;
                        }
                        }


                        I've tried all these for this line and it hasn't worked either:

                        menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");

                        _parent.menu_bar.ySlideTo (btnArray, .5, "easeOutElastic");

                        _parent.menu_bar.ySlideTo (btnArray
                        , .5, "easeOutElastic");
                        • 9. Re: How can I reduce this code?
                          Level 7
                          OK... first off, there's no sense in testing using simply btnArray in
                          the ySlideTo method call... you have to use btnArray

                          Now, the problem is scope again... btnArray is not in the scope of the
                          clip's onRelease.

                          You'll need to reference btnArray
                          using _parent.btnArray since
                          it's defined on the parent of those button clips.

                          I think the line should be:

                          _parent.menu_bar.ySlideTo (_parent.btnArray
                          , .5, "easeOutElastic");
                          • 10. How can I reduce this code?
                            blemmo Level 1
                            Guys, please note that the webforum uses an 'i' in square brackets ( [ i ] without the spaces) as markup to start italic text, so it wont show up in the posted code (in the forum's website). Please use the Attach Code button for this code, or add spaces before the brackets.

                            Josh, the line
                            var btnArray:Array = new Array[-106,-68,-28,49,88];
                            isn't right, it should be either
                            var btnArray:Array = new Array(-106,-68,-28,49,88);
                            or
                            var btnArray:Array = [-106,-68,-28,49,88];

                            When the mouse event gets defined in a loop like that, the value of 'i' is resolved in the moment the code executes. This is after the loop was run, so 'i' will be 6, but btnArray[6] is undefined. You can use the attached code to pass the 'i' value by value to the onRelease function. If you have 6 buttons, there's one value missing in the array, btnArray[5] is undefined.

                            hth,
                            blemmo