问题
I should further describe this. In my AS3, I am currently creating dynamic text from an XML sheet into a textfield. my codes looks like this to help describe this further:
function XMLLoader(e:Event):void
{
xmlData = new XML(e.target.data);
ParseList(xmlData);
}
function ParseList(nameData:XML):void
{
var nameList:XMLList = nameData.Searchtext.Name;
for each (var nameElement:XML in nameList){
directory_text.appendText(nameElement.text() + "\n");
trace(nameElement.text());
}
}
What I want to do is using the same sort of format, create a text box for EACH item outputted (in this case names) so that I can make each seperate item clickable and add a function. I was hoping to do this with "for each" but there just isnt a clear way to do it.
Any ideas? my AS3 calls in all listed under , and this can reach up to 50+ items, so I want to add it dynamically.
Thanks in advance!!
回答1:
This is a simple example of doing what you asked. This adds a TextField
for every Name node in a container Sprite
object and then adds an event listener to the container to catch any clicks that occur on the container (and it's children.)
In the event listener method, it figures out which instance of the TextField
s that was clicked and traces its contents.
You likely cannot use this code immediately, but it shows the principle of how something like this can be done.
function parseList(nameData:XML):void {
var nameList:XMLList = nameData.Searchtext.Name;
var textContainer:Sprite = new Sprite();
this.addChild(textContainer);
for each (var node:XML in nameList) {
var currTextField:TextField = new TextField();
currTextField.text = node.text();
currTextField.y = textContainer.height; //Place the textfield below any previous textfields
currTextField.height = 30;
currTextField.selectable = false;
textContainer.addChild(currTextField);
}
textContainer.addEventListener(MouseEvent.CLICK, onTextClick);
}
function onTextClick(e:MouseEvent):void {
if (e.target is TextField) {
var currTextField:TextField = e.target as TextField;
trace("This name was clicked:", currTextField.text);
}
}
来源:https://stackoverflow.com/questions/14761667/dynamically-creating-multiple-textboxes-that-are-populated-with-xml-data