7 Replies Latest reply on Oct 1, 2009 8:54 AM by Rothrock

    Shorten / Optimize repetive, long code.

    prodigalmaster Level 2

      Hi, my code works, but is way too long, infact to get the result I want it would need to be very very long.

       

      Here is *some* of my code, this is for Left. I have 3 other similar bits of code for Up, Right and Down.


      on (keyPress "<Left>") {
          _root.player.gotoAndStop("left");
          leftFunc = function () {
              if (_root.player.hitL.hitTest(_root.everything.hitAll.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll2.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll3.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll4.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll5.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll6.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll7.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll8.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll9.hitLeft) || _root.player.hitL.hitTest(_root.everything.hitAll10.hitLeft)) {
                  trace("hitL");
              } else {
                  _root.everything._x += 50;
              }
          };
          leftFunc();
      }

       

      Now what I *want* (not need) is a way to shorten this code... but im no good at (optimsing?) code so.. can anyone explain how I can?

      I remember something about loops but actually all i can remember is: i + 1  heh...

       

      Thanks

        • 1. Re: Shorten / Optimize repetive, long code.
          prodigalmaster Level 2

          If there is no simple way then just say so.

          ^^

          • 2. Re: Shorten / Optimize repetive, long code.
            Ned Murphy Adobe Community Professional & MVP

            It's not difficult, but it is complicated due to you not being consistent in the way you named the hitAll objects...  that being that you did not use a numeric value for the first, but did for all the rest.  So if you were to change that to be similar to the rest and have a number "1" suffixed to it, then you could use a loop to see if any of those hitTests were true...

             

            var hitTestCheck = false;

             

            for(i=1; i<11; i++){

                 if (_root.player.hitL.hitTest(_root.everything["hitAll"+i].hitLeft){

                      hitTestCheck = true;

                      break;

                 }

            }

             

            And then use the results of that test (hitTestCheck in this example) for the rest of your conditional checking.  You do not need to use/call a function within that as you do.

            • 3. Re: Shorten / Optimize repetive, long code.
              prodigalmaster Level 2

              Thank you very much, after messing around with it for awhile it now works

               

              Lets hope I dont speak too soon.

              • 4. Re: Shorten / Optimize repetive, long code.
                Rothrock Level 5

                And it is really hard, but it will pay off in the end if you learn to start counting with zero.

                 

                Later when you start doing loops like that and using arrays you will know why.

                1 person found this helpful
                • 6. Re: Shorten / Optimize repetive, long code.
                  prodigalmaster Level 2

                  OK, thanks for the help too, rothrock.


                  by counting from 0 you mean 'instancename0' right?

                   

                  humm... I know what arrays are.. but im too scared to learn them.

                  • 7. Re: Shorten / Optimize repetive, long code.
                    Rothrock Level 5

                    Yup that is what I mean. For some reason a lot of people (myself included) have a hard time learning to count from zero.

                     

                    Arrays are nothing scary. Just imagine one of those old post office sorting desks -- or any kind of thing that has just a bunch of little boxes in a row that you can put stuff in. That is all an array is. You can just put stuff in the boxes and then later you say, "Go to box 3 and get what is in it."

                     

                    The trick is that in Flash arrays are zero based. That means the first box you can put something in is box zero. So in the above example box 3 is actually the 4th box in the row. If you don't start counting at zero you end up subtracting one from your value to get the right array element, but then you forget that you've subtracted and so you have to add, but then you've added so you have to subtract again...and on and on...and somehow the balance gets off and you have add or subtract 2 and it just goes down hill from there.

                     

                    So here is an example, suppose you had three clips on the stage and each of them had a text box called label inside of them you could do this:

                     

                    var myNames:Array=["John","Jill","Ted"];

                     

                    for (var i=0;i<myNames.length;i++){

                    var curClip:MovieClip=this["clip"+i];

                    curClip.label.text=myNames[i];

                    }

                     

                    That way your clips are named clip0, clip1, clip2 and your array has a length three, but start counting at zero. Totally simple example, but it just gets better from there...