9 Replies Latest reply on Feb 20, 2009 8:15 AM by Ned Murphy

    Help reducing down some coding

    abimpson
      I have attached some code I'm using on a sliding scroll bar that ensures that the slidehead and the content snap to certain positions when I release the scroll bar. It all works, but I think there must be an easier way to reduce the code using a loop or some such. I'm not an action script expert, so thought I'd post here on the off-chance that anyone might be able to post some suggestions!

      Code attached, thanks for your help!
        • 1. Help reducing down some coding
          Ned Murphy Adobe Community Professional & MVP
          You need to see if you can determine a pattern in the repetitious stuff that you can convert to a more automated computation. In your if(_global.roomslide.... statements I see this pattern...

          scrollFace._x = initPosition+(unitWidth*2*(_global.roomslide-1));

          contentMain._x = initContentPos-(unitContent*(_global.roomslide-1));

          So that would reduce that entire set of if statements to two lines of code. I haven't looked at the first set, but you should try to see if you can find a pattern there.
          • 2. Re: Help reducing down some coding
            abimpson Level 1
            That's absolutely brilliant, thank you! I'm now developing the design further, and adding a gotoAndPlay to each of the original if statements as follows:

            if (_global.roomslide==1){
            scrollFace._x = initPosition;
            contentMain._x = initContentPos;
            gotoAndPlay("room1")
            };

            if (_global.roomslide==2){
            scrollFace._x = initPosition+(unitWidth*2);
            contentMain._x = initContentPos-unitContent;
            gotoAndPlay("room2")
            };

            Can i make this line more generic to follow the same pattern you suggested,
            i.e. gotoAndPlay("room_global.roomslide")

            Maybe this should be gotoAndPlay("room"_global.roomslide) ??

            Thanks!
            • 3. Re: Help reducing down some coding
              Ned Murphy Adobe Community Professional & MVP
              I'm gonna assume that _global.roomslide is the right value to use. The correct way to code the gotoAndPlay would be...

              gotoAndPlay("room"+_global.roomslide);
              • 4. Re: Help reducing down some coding
                Ned Murphy Adobe Community Professional & MVP
                Actually, the more correct way would be:

                var destination:String = "room"+String(_global.roomslide);
                gotoAndPlay(destination);

                I figure it's better to show the all the pieces than the shortcuts... helps you to see the parts involved.
                • 5. Re: Help reducing down some coding
                  abimpson Level 1
                  Briliant, thank you very much for your help! It's helped me reduce the amount of coding, and shown me a few new tricks!

                  I don't expect there's any way to reduce the amount of coding for the first part, as far as I can see, so I think that's it all sorted!
                  • 6. Re: Help reducing down some coding
                    Ned Murphy Adobe Community Professional & MVP
                    Is this reverse psychology of some sort? A challenge to me to attack the first pile of stuff too? I'll take a look at it and let you know if I find a way, but I may not tell you what it is if I do. That'd be cheating you of an opportunity to solve a puzzle on your own.
                    • 7. Help reducing down some coding
                      Ned Murphy Adobe Community Professional & MVP
                      Yep, I can reduce that first batch to the form shown below... though the end points that it deals with may or may not play out. You'll know better than me if you can solve what's in the if(...) line... But if they don't you could still put conditionals on the value of i within the loop to handle them.

                      • 8. Re: Help reducing down some coding
                        abimpson Level 1
                        I actually hadn't expected a solution, but thanks to your suggestion I came up with the attached code which works perfectly! Thanks so much for your help!
                        • 9. Re: Help reducing down some coding
                          Ned Murphy Adobe Community Professional & MVP
                          You got it! and you're welcome.