2 Replies Latest reply on May 4, 2006 9:12 AM by Kevin_Favro

    Creating Dynamic Text Fields

      Hey there,
      I'm reading in an XML file, and then want to be able to create a list of song names.

      I'm using a function to loop through the XML file and create a text field for each entry.

      Here's my function:
      function createMenu(xmlFile,medType,medCat) {
      var items = xmlFile.firstChild.childNodes; // xml file -> music -> songs array
      var count = 0;
      var spacing = 20;
      for(i=0;i<items.length;i++) {
      if(items .nodeName == medType) {
      .attributes.category == medCat) {
      var sTitle = items .firstChild.firstChild;
      var path = items
      var songName:String = "songName"+i;

      var t:TextFormat = new TextFormat();
      t.color = 0xFFFFFF;
      t.size = 12;
      t.font = "BitStream Vera Sans";

      eval(songName)._y = count * spacing;
      eval(songName).text = sTitle +" - "+path;

      My Question:
      1) Why do I have to use the eval() function every time I want to set the property of my new text field? I've tried many different ways, and only eval() works.
      2) I know one of my nodes coming from the XML file is going to be very long. How (or can I) set my new text field to "wrap"? Right now my text fields are set up for a width of 300, so this long node is getting cut off, and I want it to drop to the next line.

        • 1. Re: Creating Dynamic Text Fields
          kglad Adobe Community Professional & MVP
          1. songName is a string. flash needs help resolving strings into objects. you can use eval() or bracket notation. an example of the the latter: this[songName]._y =...

          2. enable the multiline and wordWrap property of you textfield. you may want to play with autoSize to check its effect, too.
          • 2. Re: Creating Dynamic Text Fields
            Kevin_Favro Level 1
            thanks klad!

            i figured that songName was a string, and that's why I needed the eval...but is there any way to shortcut this? Just trying to see if I can make my code any cleaner...i tried this:

            var songName:String = "songName"+i;
            songName = eval(songName);

            and that didn't seem to work. is that because songName is still ultimately a string? if I took the strict type declaration off of songName, would that help it? I'm just curious as to whether or not what I already have written is standard in actionscript, or if there's a more elegant way to accomplish this.