Skip to main content
Inspiring
October 21, 2010
Answered

Arrays

  • October 21, 2010
  • 1 reply
  • 818 views

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);

This topic has been closed for replies.
Correct answer Ned Murphy

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


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, data: fontArray1});

}

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], data:  this["fontArray"+k]});

     }

}

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, data: fontArray1});

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.

1 reply

Ned Murphy
Legend
October 21, 2010

Using arrays for what?

Inspiring
October 21, 2010

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...

Ned Murphy
Legend
October 21, 2010

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.