7 Replies Latest reply on Jun 8, 2006 9:38 AM by >Vee<

    how to loop through attributes

    >Vee< Level 1
      Im trying to loop through some XML and use the two attributes name and href for each button im creating. so far all I have been able to do is onfuse myself, well not totally - I can pull the first name attribute from the firstChild. But I have not been able to successfully assign it to a button. I took out my button code as it was probably wrong. Heres what Ive tried...
        • 1. Re: how to loop through attributes
          blemmo Level 1
          Have a look at the attached code. It goes through the childnodes and assigns the 'name' and 'href' attribute to a button's 'text' property (may be a variable assigned to a textfield in that button) and it's onRelease function. The link is also stored in a 'link' property of that button. It assumes the buttons have indexed names: button0, button1 etc., that represent the index of the childnode, and are located in _root.
          If you also want an array with the found values, uncomment the lines with 'menus'. This will add objects with a 'name' and 'link' property to the 'menus' array.

          hth,
          blemmo

          • 2. how to loop through attributes
            >Vee< Level 1
            Well that works well, and simple too. Thank you. I had two more questions if you don't mind.
            1. If I had a btn that wasnt part of this loop, how would I assign one of the attributes (say the 'href') held in the loop to it. Im noticing that when I trace the menus array from outside the loop - it traces blank. Normally I would go:
            random_btn.onRelease = function(){
            mc_loader.loadClip(" http://" + menus[0], "content_mc");
            }

            2. Im also trying to use the submenus that are in the XML. I tried the following, but didnt have the same results as before. I put my could at the inner part of the loop with an extra childNodes to go deeper into IF they had the following setup, could I pull the subMenu in each with the following:
            • 3. Re: how to loop through attributes
              blemmo Level 1
              The arrays are built in an event, so before that event happens, they are undefined. After the event, you can access the arrays as usual. The trace(menus); call is before the myXML_xml.load("menu2.xml"); line, which triggers the loading, so it can't trace any values. It also wouldn't trace anything when placed directly after the load() call, because the loading will take some time until all data is loaded and the onLoad event is fired. That makes it a bit hard to use the data outside the event, because you must assert that it's loaded when used. The onLoad event is just perfect for that, so it should be done there, just after the loop through the xml nodes.
              For the submenus, the attached code will place the childnode data in subMenus. The arrays must be declared outside the event, otherwise they get deleted when the event finishes.

              cheers,
              blemmo
              • 4. how to loop through attributes
                >Vee< Level 1

                blemmo,
                Thanks for explaining everything so well. I have to say that i have never seen that way of tracing it and Im just beginning to get it. Im trying my best to use alot more arrays in my projects and this form of it is just so new to me. Again thanks for your time.
                best
                Vee
                • 5. Re: how to loop through attributes
                  >Vee< Level 1
                  Blemmo,
                  I noticed that my submenus kept rewriting over eachother after they got past the counter "2". I traced it and this is why
                  sub0
                  sub1
                  sub2
                  sub0
                  sub1
                  The instances reset to O everytime it switches nodes. I tried uses an attachMovie but the same thing kept happening (only three appear). should I have a setup that is more like a movie clip as a container for the main nav. btn and sub menus, this way each acts like a container for the "menu" element.
                  Name it like: menu_mc
                  Inside menu_mc: nav_main_mc & nav_sub_mc
                  Lemme me what you think.

                  • 6. how to loop through attributes
                    blemmo Level 1
                    Ahh... of course this must happen, because j starts at 0 for each childnode, so we can't use j as part of the name for the sub MCs. So instead of j, we can use the length of the submenus array. In the attached code, the names will start at 1, not 0 as in the first code. If you want to start at 0, just substract 1 from the array length.

                    greets,
                    blemmo




                    • 7. Re: how to loop through attributes
                      >Vee< Level 1
                      Blemmo,
                      your f*n awsome. thanx. It worked great.
                      Reviewing the script that kept swtiching back to 0 everytime it switched nodes, would it work to have a conditional in there that told it to switch from menuHolder0_mc to next menuHolder1_mc?
                      The movieclip menuHolder_mc is a container for the buttons button_btn and sub_btn. Button_btn is the main navigation buttons wile sub_btn is the its sub menus.
                      Again thanks for the lessons. Its a big help.