11 Replies Latest reply on Mar 5, 2008 11:37 AM by Newsgroup_User

    Condense Code?

    charner
      I am only mildly proficient with Actionscript 2. I have all the code for this website on one frame, and it seems like it would be easy to take the following code and condense it. What I need is a code that someone makes it so that I can refer to WHICHEVER movie clip is on frame 7 (only one will be on it at a time) or something like that (like, whichever movie clip is visible. I don't know! Just SOMETHING) and tell whichever one that might be to do the following code. The way I have it now, I have to paste this code after each and every button that says "on release do the following". I hope this make sense.

      Here is a sample of the "on release code" that is prior to the following code. This is found 8 times, as I have eight menu options.

      menu_mc.home_btn.onRelease = function() {

      The code that I have below each of the button "on release" functions is attached.


      The code WORKS fine. I just want to learn how to be more proficient. Any ideas?

      Thanks so much!
      Christy
        • 1. Re: Condense Code?
          Level 7
          Christy,

          Sure thing! Every one of these code blocks is nearly identical. The
          only difference I see is a) the instance name of the movie clip and b) the
          instance name of the button. Fortunately, you've used a very cosistent
          naming scheme, so you can put the beginning portion of each instance name
          into an array and use a for() loop to step through your code blocks.

          Check it out.

          var instanceNames:Array = new Array("home", "holyspirit", "globalrevival");

          I know you're using more than this, but the nice thing about using an
          array is that you can add elements to it at anytime. Adding
          "ministrylocations," for example, would look like this:

          var instanceNames:Array = new Array("home", "holyspirit", "globalrevival",
          "ministrylocations");

          So now you have an array, arbitrarily named instanceNames. Arrays start
          counting at zero, so instanceNames[0] refers to the first slot, "home";
          instanceNames[1] refers to the second slot, "holyspirit"; and so on. To
          actually *use* these strings, you're going to have to convert them into
          object references, with the addition of other string portions, such as "_mc"
          and the like.

          Again, because of your consistent naming scheme, the pattern is pretty
          straightforward. This first block ...

          if (home_mc._currentframe=="7") {
          home_mc.gotoAndPlay("fadeout");
          menu_mc.home_btn.enabled = true;
          homecontent_mc._visible=false;
          }

          ... can be seen as this:

          if (***_mc._currentframe=="7") {
          ***_mc.gotoAndPlay("fadeout");
          menu_mc.***_btn.enabled = true;
          ***content_mc._visible=false;
          }

          ... where *** represents the word "home," but later represents the words
          "holyspirit," "globalrevival," and so on. You're going to be replacing the
          *** with references to your instanceNames array; for example ...

          if (instanceNames[0]_mc._currentframe=="7") {
          instanceNames[0]_mc.gotoAndPlay("fadeout");
          menu_mc.instanceNames[0]_btn.enabled = true;
          instanceNames[0]content_mc._visible=false;
          }

          ... only that isn't enough, just yet. Your array holds a series of strings,
          and each string needs to be concatenated (combined) with related strings
          like "_mc", "_btn", and the like. Even when they're concatenated, those
          strings will only be strings -- but they need to be object references. You
          can use the array access operator to interpret the object references for
          you.

          The array access operator ([]) needs an object reference prefix (some
          existing object), so in this case, since all of this code appears in a frame
          of the main timeline, you can use "this":

          this[instanceName[0] + "_mc"]._currentframe == 7

          Ultimately, that translates out to this:

          this["home" + "_mc"]._currentframe == 7
          this["home_mc"]._currentframe == 7
          this.home_mc._currentframe == 7
          home_mc._currentframe == 7

          The above four lines are all equivalent in this context.

          Obviously, you couldn't juse use 0 as your instanceNames index, because
          0 always points ot the first element. Instead, you'll let the value of 0 be
          determined by a variable that gets incremented in a for() loop.

          for (var i:Number = 0; i < instanceNames.length; i++) {
          if (this[instanceNames + "_mc"]._currentframe == "7") {
          this[instanceNames
          + "_mc"].gotoAndPlay("fadeout");
          menu_mc.[instanceNames + "_btn"].enabled = true;
          this[instanceNames
          + "content_mc"]._visible = false;
          }
          }

          Look carefully over the above block. Inside the if() statement, the
          expression is the same as you've seen -- this[instanceNames[0] + "_mc"] --
          and so it is also within the line that follows. In the menu_mc line, the
          expression changes just a bit, because the operative portion of the code has
          a different parent object. Not this, but menu_mc; plus, the "_mc" string
          has been changed to "_btn". Finally, the last line changes that extra
          string to "content_mc" and returns to this.

          The reason you can continue to add items to your array is because of the
          reference to instanceNames.length, where the Array.length property (as
          invoked on instanceNames) returns the length of the array itself (the number
          of elements it contains). If you add items, that property's value is
          adjusted accordingly.

          Here's a bit more info on the process I discussed above:

          http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          "charner" <webforumsuser@macromedia.com> wrote in message
          news:fq98r2$7i0$1@forums.macromedia.com...
          >I am only mildly proficient with Actionscript 2. I have all the code for
          >this
          > website on one frame, and it seems like it would be easy to take the
          > following
          > code and condense it. What I need is a code that someone makes it so that
          > I can
          > refer to WHICHEVER movie clip is on frame 7 (only one will be on it at a
          > time)
          > or something like that (like, whichever movie clip is visible. I don't
          > know!
          > Just SOMETHING) and tell whichever one that might be to do the following
          > code.
          > The way I have it now, I have to paste this code after each and every
          > button
          > that says "on release do the following". I hope this make sense.


          • 2. Re: Condense Code?
            charner Level 1
            Hey, thanks so much for your very quick and very easy to read response!

            Unfortunately, I am still stuck, although I think its due to my inexperience a lot more than your explanation, which was really good. So, before, my code for an entire section read like this:

            menu_mc.holyspirit_btn.onRelease = function() {

            //change color//
            var colo:Color = new Color(square_mc);
            colo.setRGB(0xffffff);

            //change position of square//
            targX = 633;
            targY = 280;
            targW = 267;
            targH = 329;
            targA = 30;


            //make content on _root show up//
            holyspiritcontent_mc._visible = true;

            //make main main movie clip correct visibility//
            holyspirit_mc._visible=true;
            holyspirit_mc.gotoAndPlay("fadein");

            //disable button//
            menu_mc.holyspirit_btn.enabled=false;

            ------ORIGINAL CODE BELOW-------------

            //"fade out" code//

            if (home_mc._currentframe=="7") {
            home_mc.gotoAndPlay("fadeout");
            menu_mc.home_btn.enabled = true;
            homecontent_mc._visible=false;
            }

            if (holyspirit_mc._currentframe=="7") {
            holyspirit_mc.gotoAndPlay("fadeout");
            menu_mc.holyspirit_btn.enabled = true;
            holyspiritcontent_mc._visible=false;
            }

            if (globalrevival_mc._currentframe=="7") {
            globalrevival_mc.gotoAndPlay("fadeout");
            menu_mc.globalrevival_btn.enabled = true;
            globalrevivalcontent_mc._visible=false;
            }

            if (ministrylocations_mc._currentframe=="7") {
            ministrylocations_mc.gotoAndPlay("fadeout");
            menu_mc.ministrylocations_btn.enabled = true;
            ministrylocationscontent_mc._visible=false;
            }
            if (resources_mc._currentframe=="7") {
            resources_mc.gotoAndPlay("fadeout");
            menu_mc.resources_btn.enabled = true;
            resourcescontent_mc._visible=false;
            }

            if (about_mc._currentframe=="7") {
            about_mc.gotoAndPlay("fadeout");
            menu_mc.about_btn.enabled = true;
            aboutcontent_mc._visible=false;
            }

            if (donate_mc._currentframe=="7") {
            donate_mc.gotoAndPlay("fadeout");
            menu_mc.donate_btn.enabled = true;
            donatecontent_mc._visible=false;
            }

            else if (contactus_mc._currentframe=="7") {
            contactus_mc.gotoAndPlay("fadeout");
            menu_mc.contactus_btn.enabled = true;
            contactuscontent_mc._visible=false;
            }


            };



            Now, thanks to you, it looks like this:



            //------------HOME--------------//
            menu_mc.home_btn.onRelease = function() {

            //change color//
            var colo:Color = new Color(square_mc);
            colo.setRGB(0x54331F);

            //change position of square//
            targX = 605;
            targY = 260;
            targW = 333;
            targH = 326;
            targA = 80;

            //make content on _root show up//
            homecontent_mc._visible = true;

            //make main main movie clip correct visibility//
            home_mc._visible=true;
            home_mc.gotoAndPlay("fadein");

            //disable button//
            menu_mc.home_btn.enabled=false;

            ------------NEW CODE BELOW-----------

            //set a variable//
            var instanceNames:Array = new Array("home", "holyspirit", "globalrevival", "ministrylocations", "resources", "about", "donate", "contactus");


            for (var i:Number = 0; i < instanceNames.length; i++)
            {
            if (this[instanceNames + "_mc"]._currentframe == "7")
            {
            this[instanceNames + "_mc"].gotoAndPlay("fadeout");
            menu_mc[instanceNames + "_btn"].enabled = true;
            this[instanceNames + "content_mc"]._visible = false;
            trace ("this array thing worked");
            }
            else {
            trace ("I must be stupid");
            }
            }



            };

            Now, when I test it out, its just popping up with "I must be stupid" 8 times. Any idea why? (Besides the obvious, haha.)

            Again, thanks a TON for all your amazing help!

            Christy Text
            • 3. Re: Condense Code?
              Level 7
              Christy,

              > Now, when I test it out, its just popping up with "I must be stupid"
              > 8 times. Any idea why? (Besides the obvious, haha.)

              haha ... Well, the whole thing hinges on whether or not each clip's
              current frame is equal to 7. As the if() statement runs through its course
              8 times, it checks if each movie clip's current frame is 7. If it is, it
              does some stuff; but if it isn't -- each, in turn -- it traces "I must be
              stupid" to the Output panel.

              You could use trace() to figure a few things out, while you're
              debugging. For example, try this:

              for (var i:Number = 0; i < instanceNames.length; i++) {
              // this will show what frame the clip is actually on
              trace(this[instanceNames + "_mc"]._currentframe);
              if (this[instanceNames
              + "_mc"]._currentframe == 7) {
              this[instanceNames + "_mc"].gotoAndPlay("fadeout");
              menu_mc[instanceNames
              + "_btn"].enabled = true;
              this[instanceNames + "content_mc"]._visible = false;
              trace(instanceNames
              + "_mc was on frame 7");
              } else {
              trace(instanceNames + "_mc was not on frame 7");
              }
              }

              I also noticed, just now, that the if() statement was checking if the
              frame was "7" (in quotes), rather than 7 (a number). That may or may not
              hurt the situation.

              Give those new traces a try and let me know what happens. I'll stick
              with you until we figure it out.


              David Stiller
              Contributor, How to Cheat in Flash CS3
              http://tinyurl.com/2cp6na
              "Luck is the residue of good design."


              • 4. Re: Condense Code?
                charner Level 1
                I've been doing tons of tests to see what could be wrong, but here's the results from your suggested trace:

                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                undefined
                home,holyspirit,globalrevival,ministrylocations,resources,about,donate,contactus_mc was not on frame 7
                • 5. Re: Condense Code?
                  charner Level 1
                  Oh, and its not a "current frame" problem. I checked a bunch of different ways (and yes, I did double check to make sure that things were actually ON frame 7).
                  • 6. Re: Condense Code?
                    Level 7
                    Christy,

                    Aha! Okay ... I see what happened, and your trace()-based
                    troubleshooting is what brought it to light.

                    The stupid one here is clearly me. ;) I wasn't thinking when I used i
                    as the counter variable for that for() loop. The problem is -- and this
                    happens on the browser-based version of the web forum (I used an NNTP
                    client) -- when the forum sees a bracket [, followed by an i, followed by
                    another bracket ], the whole thing is construed as the equivalent if the
                    italic (i) tag in HTML. < i >

                    You can see that happening in one of my previous answers, where suddenly
                    everything turns to italics. This sort of thing tends not to happen when
                    you access the forums outside of a browser, which is almost always what I
                    do. Okay, wow ... well, I hope this hasn't muddied up the waters for you.
                    I'm going to replace that i variable with x, because there isn't any such
                    thing as an <x> tag in HTML, and let's see if that makes everything suddenly
                    snap into focus.

                    var instanceNames:Array = new Array("home", "holyspirit", "globalrevival",
                    "ministrylocations", "resources", "about", "donate", "contactus");

                    for (var x:Number = 0; x < instanceNames.length; x++) {
                    if (this[instanceNames[x] + "_mc"]._currentframe == 7) {
                    this[instanceNames[x] + "_mc"].gotoAndPlay("fadeout");
                    menu_mc[instanceNames[x] + "_btn"].enabled = true;
                    this[instanceNames[x] + "content_mc"]._visible = false;
                    trace ("this array thing worked");
                    } else {
                    trace ("I must be stupid");
                    }
                    }

                    Do you see the difference? In your code, the most important part was
                    missing -- not your fault -- which was the array access operator, [], being
                    used a second time inside each expression. The fact that your trace lists
                    *all* the array contents every time is because you were referencing the
                    array itself, rather than a given element of the array. You did that
                    because my own code was altered thanks to my careless use of the variable i.
                    Does that make sense? I hope I haven't muddled the whole thing for you!


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: Condense Code?
                      charner Level 1
                      Thanks so much for your help. I tried the fixes you mentioned, added in a bit more testing, and it gets stuck at the trace "kinda". I think something's wrong with the actual variable or something. I don't know what to do! If I can't get this part figured out, I'll probably just do it the long way. Thanks for all the help you've given so far. At this point, I really just need to learn more about arrays and variables, don't I?

                      Thanks again. Here's the code I tested (and got stuck with the trace "kinda" coming up 8 times.

                      var instanceNames:Array = new Array("home", "holyspirit", "globalrevival",
                      "ministrylocations", "resources", "about", "donate", "contactus");

                      for (var x:Number = 0; x < instanceNames.length; x++) {
                      if (this[instanceNames[x] + "_mc"]._currentframe == 7) {
                      this[instanceNames[x] + "_mc"].gotoAndPlay("fadeout");
                      menu_mc[instanceNames[x] + "_btn"].enabled = true;
                      this[instanceNames[x] + "content_mc"]._visible = false;
                      trace ("this array thing worked");
                      }
                      else if (holyspirit_mc._currentframe == "7"){
                      trace ("kinda");
                      }
                      else {
                      trace ("I must be stupid");
                      }
                      }
                      • 8. Re: Condense Code?
                        charner Level 1
                        Oh, and on a side note: making it "7" or 7 doesn't change anything for the currentframe == part.

                        • 9. Re: Condense Code?
                          charner Level 1
                          YAY! I got it to work!!!!!!

                          The only thing that I think was messing it up was actually that it said "this" rather than "_root". So, thanks a bunch for all your help, and now I've learned very valuable actionscript! Check out the website you helped create around April 1st: http://www.fredvehe.com (the site you see now is not the new one).

                          Thanks so much,
                          Christy Harner
                          http://www.curraheegraphics.com
                          • 10. Re: Condense Code?
                            Level 7
                            Christy,

                            Let's imagine the current frame of holyspirit_mc happens to be frame 7.

                            Now ActionScript encounters a for() loop. It's going to jog around this
                            loop eight times, because there are eight elements in the instanceNames
                            array. So far, so good, right?

                            FIRST LAP:

                            This expression: this[instanceNames[x] + "_mc"]
                            ... resolves to this movie clip: home_mc

                            Therefore, the if() statement checks if home_mc is on frame 7. In this
                            scenario, the answer is false, because holyspiric_mc is the one on frame 7.
                            So the if() statement goes to its else..if clause, which checks directly if
                            holyspirit_mc is on frame 7.

                            It is! So the trace() "kinda" appears in the Output panel. The last
                            else clause is skipped.

                            SECOND LAP:

                            This expression: this[instanceNames[x] + "_mc"]
                            ... resolves to this movie clip: holyspirit_mc

                            Therefore, the if() statement checks if holyspirit_mc is on frame 7.

                            It is! So this time, no "kinda" trace occurs.

                            THIRD LAP:

                            This expression: this[instanceNames[x] + "_mc"]
                            ... resolves to this movie clip: globalrevival_mc

                            Therefore, the if() statement checks if globalrevival_mc is on frame 7.
                            In this scenario, the answer is false, because holyspiric_mc is the one on
                            frame 7. So the if() statement goes to its else..if clause, which checks
                            directly if holyspirit_mc is on frame 7.

                            It is! So the trace() "kinda" appears in the Output panel. The last
                            else clause is skipped.

                            FOURTH LAP:

                            This expression: this[instanceNames[x] + "_mc"]
                            ... resolves to this movie clip: ministrylocations_mc

                            Therefore, the if() statement checks if ministrylocations_mc is on frame
                            7. In this scenario, the answer is false, because holyspiric_mc is the one
                            on frame 7. So the if() statement goes to its else..if clause, which checks
                            directly if holyspirit_mc is on frame 7.

                            It is! So the trace() "kinda" appears in the Output panel. The last
                            else clause is skipped.

                            ... AND SO ON. Which means you will indeed see seven "kinda" traces in
                            the Output panel. Does that make sense?


                            David Stiller
                            Adobe Community Expert
                            Dev blog, http://www.quip.net/blog/
                            "Luck is the residue of good design."


                            • 11. Re: Condense Code?
                              Level 7
                              Christy,

                              > YAY! I got it to work!!!!!!

                              Ah, good stuff! :)


                              David Stiller
                              Contributor, How to Cheat in Flash CS3
                              http://tinyurl.com/2cp6na
                              "Luck is the residue of good design."