Search code examples
flashactionscriptaddchild

AS3 addChild with TextFields only showing the last one added


I'm building a Facebook client in actionscript, and I'm running into a problem. I have this block of code:

        var loader:URLLoader = URLLoader(event.target);

        var feedResponse:String = (event.target.data);
        var object:Object = JSON.decode(feedResponse); //getting the data

        var friendsStatus:Array = new Array(); //array for status'
        var friendsName:Array = new Array(); //array for names

        var startX:int = 120;
        var startY:int = 100;

        var myFormat:TextFormat = new TextFormat();
        myFormat.size = 16;

        var statusMessage:TextField = new TextField();
        statusMessage.defaultTextFormat = myFormat;
        statusMessage.wordWrap = true;
        statusMessage.width = 600;

        var faceFriend:TextField = new TextField();
        faceFriend.defaultTextFormat = myFormat;
        faceFriend.wordWrap = true;
        faceFriend.width = 300;

        for(var i:int = 0; i < 10; i++)
        {
            if(object.data[i].message == undefined){
                var userStory:String = object.data[i].story; // mowing through the facebook JSON response
                friendsStatus[i] = userStory;
            } else {
                var userStatus:String = object.data[i].message;
                friendsStatus[i] = userStatus;
            }

            var fName:String = object.data[i].from.name;
            friendsName[i] = fName;
        } 

        for(var j:int; j < 10; j++) // The headache loop
        {
            faceFriend.x = startX;
            faceFriend.y = startY;

            faceFriend.text = friendsName[j]; //assign the current name to the TextField text property
            addChild(faceFriend); //add it to the stage
            startY += 30; //increase the y value so they dont overlap
            trace(startY);
            trace(faceFriend.text);
            trace("");

            statusMessage.x = startX;
            statusMessage.y = startY;

            statusMessage.text = friendsStatus[j]; //same here for the status'
            addChild(statusMessage);
            startY += 20;
            trace(startY);
            trace(statusMessage.text);
            trace("");
        }

which is supposed to loop through a set of status', assign them to variables and add them to the stage. However, all I achieve is the very last status being added to the stage. As you can see from all the traces, I have discovered that all the arrays have the correct information, and the y value (so its not all on top of each other) is progressing correctly. It just refuses to let anything but the final TextField be visible :(

Any ideas?

P.S. I realise it's not exactly brilliant code - sorry for the headache in advance :P


Solution

  • You need to create

    var faceFriend:textField inside the loop.
    

    What you are currently doing is creating one text field and manipulating it over and over again. Which is causing a problem. This code will generate 10 fields and align them horizontally.

    var faceFriend:TextField;
    
    for(var i:uint;i<10;i++)
    {
        faceFriend = new TextField();
        faceFriend.x = i*20;
        addChild(faceFriend);
    }
    

    whereas this code will generate one field manipulate it 10 times and show you one field , which is essentially what you are doing.

    var facefriend:TextField = new TextField();
    
     for(var i:uint;i<10;i++)
     {
        faceFriend.x = i*20;
        addChild(faceFriend);
     }
    

    This