8 Replies Latest reply on Oct 21, 2010 9:09 AM by Ned Murphy

    Arrays

    Ron Colmen Level 2

      I've got my_list1, my_list2, my_list3, up to my_list8. each my_list holds similar fonts ("fnt1", "fnt2"), etc.... How to code this using arrays?

       

      my_list1.addItem("fnt1", "fnt1");

      my_list1.addItem("fnt2", "fnt2");

      my_list1.addItem("fnt3", "fnt3");

      my_list1.addItem("fnt4", "fnt4");

      my_list1.addItem("fnt5", "fnt5");

       

      var my_fmt:TextFormat = new TextFormat();

      my_fmt.font = "fnt5";

      var my_listener:Object = new Object();

      my_listener.change = function() {

      my_fmt.font = my_list1.selectedItem.data;

      my_Dyn_Txt1.setTextFormat(my_fmt);

      my_Dyn_Txt1.embedFonts = true;

      };

      my_list1.addEventListener("change", my_listener);

      my_Dyn_Txt1.setNewTextFormat(my_fmt);

        • 1. Re: Arrays
          Ned Murphy Adobe Community Professional & MVP

          Using arrays for what?

          • 2. Re: Arrays
            Ron Colmen Level 2

            To shorten the code so that I don't have to repeat the fonts used in my_lists.

             

            my_lists: my_list1, my_list2, my_list3, up to my_list8

             

            fonts: "fnt1", "fnt2" etc...

            • 3. Re: Arrays
              Ned Murphy Adobe Community Professional & MVP

              Which code are you talking about as far as repetition goes?  I can't say that I see it.  If you mean the way you have addItem calls one after another, an array won't change that by itself, but it could be useful in reducing that using a loop.

              • 4. Re: Arrays
                Ned Murphy Adobe Community Professional & MVP

                And if you do mean the addItem repetitions, you can eliminate them by using a data provider array, but you still need to fill up the array with probably as much if not more code...

                 

                var my_fonts:Array = new Array({label:"fnt1", data:"fnt1"},{label:"fnt2", data:"fnt2"});

                my_list1.dataProvider = my_fonts;

                • 5. Re: Arrays
                  Ron Colmen Level 2

                  Thanks Ned.

                   

                  Actually I this is the area I wanted to be shortened.

                   

                  my_list1.addItem("fnt1", "fnt1");

                  my_list1.addItem("fnt2", "fnt2");

                  my_list1.addItem("fnt3", "fnt3");

                  my_list1.addItem("fnt4", "fnt4");

                  my_list1.addItem("fnt5", "fnt5");

                   

                  //then the 2nd my_list

                  my_list2.addItem("fnt1", "fnt1");

                  my_list2.addItem("fnt2", "fnt2");

                  my_list2.addItem("fnt3", "fnt3");

                  my_list2.addItem("fnt4", "fnt4");

                  my_list2.addItem("fnt5", "fnt5");

                   

                  //then the 3rd my_list

                  my_list3.addItem("fnt1", "fnt1");

                  etc...

                   

                  //up to the 8th my_list

                  • 6. Re: Arrays
                    Ned Murphy Adobe Community Professional & MVP

                    As I mentioned, you can use a loop that feeds from an array to reduce that.  Without the loop you wouldn't reduce it.

                     

                    var fontArray1:Array = new Array("font1","font2", etc...);

                     

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

                         my_list1.addItem({label: fontArray1[i], data: fontArray1[i]});

                    }

                     

                    And you could extend that to be a loop within loop to take care of all the lists, where the outer loop changes wihich list you load into and the inner fills each in turn...

                     

                    var fontArray1:Array = new Array("font1","font2", etc...);

                    var fontArray2:Array = new Array("font6","font7", etc...);

                    etc... thru fontArray8

                     

                    for(k=1; k<9; k++){

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

                              this["my_list"+k].addItem({label: this["fontArray"+k][i], data:  this["fontArray"+k][i]});

                         }

                    }

                     

                    If the same array would be used for each, then in the inner loop you would just use...

                    this["my_list"+k].addItem({label: fontArray1[i], data: fontArray1[i]});

                     

                    And you should still look into the dataProvider as an alternative for using an array to load the Lists.  If they all use the same array, that would make quicker coding of it than your original code as well, though probably not to the extent of the loop.

                    • 7. Re: Arrays
                      Ron Colmen Level 2

                      Thanks Ned! 

                      • 8. Re: Arrays
                        Ned Murphy Adobe Community Professional & MVP

                        You're welcome