A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Creating unique textFields in a For loop.

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    12

    Creating unique textFields in a For loop.

    I have a for loop that runs depending on the length of an XML file. What I want to do is create a unique text field for each run through of the loop, and give it it's own position on the stage, and finally assign it the text from an external text file that is specified in the XML file. Everything I know how to do except creating the textfields.

    for(var i:int = 0; i < blogXMLList.length(); i++)
    {
    var txtHolder:TextField = new TextField();
    txtHolder.x = textX;
    txtHolder.y = textY;
    txtHolder.width = textX;
    txtHolder.height = textY;
    txtHolder.multiline = true;
    txtHolder.wordWrap = true;
    textX += 50;
    textY += 50;
    addChild(txtHolder);

    blogPostRequest = new URLRequest(blogXMLList[i].attribute("file"));
    blogPostLoader.load(blogPostRequest);
    blogPostLoader.addEventListener(Event.COMPLETE, loadTXT);

    function loadTXT(event:Event):void
    {
    txtHolder.text = event.target.data;
    }
    }
    This code creates only one Text Field, but then replaces it's text with new text with each run through. Please help, this has been annoying me for quite a while.
    Last edited by AffinityFX; 10-28-2008 at 10:02 AM.

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Okay, this is untested code but I think it'll work for you. Try this:

    Code:
    var txtFArr:Array = new Array();
    var txtNum:Number;
    for(var i:int = 0; i < blogXMLList.length(); i++) {
    txtFArr.push(new TextField());
    txtFArr[i].x = textX;
    txtFArr[i].y = textY;
    txtFArr[i].width = textX;
    txtFArr[i].height = textY;
    txtFArr[i].multiline = true;
    txtFArr[i].wordWrap = true;
    textX += 50;
    textY += 50;
    addChild(txtFArr[i]);
    
    blogPostRequest = new URLRequest(blogXMLList[i].attribute("file"));
    blogPostLoader.load(blogPostRequest);
    blogPostLoader.addEventListener(Event.COMPLETE, loadTXT);
    txtNum = i;
    }
    
    function loadTXT(event:Event):void {
    txtFArr[txtNum].text = event.target.data;
    }
    Give that a whirl and let me know what happens.

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Posts
    12
    Thanks for your reply! Yeah it worked, the code created a new TextField for each node within an XML file, which is exactly what I need! The only problem was that for some reason it was only filling the last TextField with any text, and I realised that it was because by the time the specified text was loaded, the for loop had completed and run through again, therefore overwriting the previously loaded file (at least that's what I assumed happened.) So by creating a new URLLoader for each run through of the loop and also by passing the value of i to event listener function, it loads everything perfectly and puts it in the correct textfield.

    for(var i:int = 0; i < blogXMLList.length(); i++) {

    //Create a TextField for each node in the XML
    txtFArr.push(new TextField());
    txtFArr[i].x = textX;
    txtFArr[i].y = textY;
    txtFArr[i].width = 100;
    txtFArr[i].height = 200;
    textX += 100;
    txtFArr[i].multiline = true;
    txtFArr[i].wordWrap = true;
    txtFArr[i].border = true;
    addChild(txtFArr[i]);

    //Load the correct file and load the funtion to place the text in the correct field
    blogPostRequest = new URLRequest(blogXMLList[i].attribute("file"));
    blogPostLoader = new URLLoader();
    blogPostLoader.load(blogPostRequest);
    blogPostLoader.addEventListener(Event.COMPLETE, loadTextIntoField(i));
    }

    function loadTextIntoField(numberToLoad:int):Function {
    return function(event:Event):void {
    txtFArr[numberToLoad].text = event.target.data;
    }
    }
    Thanks again for your help!

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    No problem, glad you got it working.

  5. #5
    Junior Member
    Join Date
    Jan 2008
    Posts
    14

    post the rest

    Hey there- I'm interested in how you came to this conclusion. Clearly the XML is not external. Can you post that portion of the code as well? Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center