Skip to main content
May 4, 2006
Question

Creating Dynamic Text Fields

  • May 4, 2006
  • 1 reply
  • 286 views
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) {
if(items
.attributes.category == medCat) {
var sTitle = items .firstChild.firstChild;
var path = items
.childNodes[1].firstChild;
var songName:String = "songName"+i;

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

this.createTextField(songName,this.getNextHighestDepth(),292,100,300,200);
eval(songName)._y = count * spacing;
count++;
eval(songName).text = sTitle +" - "+path;
eval(songName).setTextFormat(t);
}
}
}
}

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.

Thanks!
This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 4, 2006
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.
May 4, 2006
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.

Thanks!

kevin