tags:

views:

326

answers:

2

I am adding DisplayObjects to a Canvas using

ContentContainer.addChild(c);

Where ContentContainer is my Canvas object and c is the DisplayObject I have created at run-time

Some of these DisplayObjects also have children of there own which are added at run-time prior to the DisplayObject being added to the Canvas

I then need to iterate over all the children of ContentContainer but the first time I do this, it says that ContentContainer has no children (ie, ContentContainer.numChildren = 0). If I do it again tho then it is fine and returns the correct number of children.

Is there something I need to call to get ContentContainer to recalculate how many children it has?

A: 

I did a similar task with a callLater in order to wait till after a dragdrop completed before recalculating some tasks. This might work for you.

public function myFunct():void{
    //do your adding
    callLater(
        function():void{
            //do your loop
        }
    )
}
invertedSpear
Hi, thanks for that. Unfortunately I don't think timing is the issue here because once the items are added, the iteration through them doesn't happen until the user clicks a button. No matter how I long I wait between loading the items and clicking the button, I still get the same problem
Addsy
+2  A: 

As Michael noted it would be helpful to see the code but you might want to look into the Event overview and About the creation policy sections in the docs - http://livedocs.adobe.com/flex/3/html/help.html?content=containers_intro_3.html

Specifically the childAdd event might be what you want to listen for before you iterate over it:

add Dispatched by a component after the component has been added to its container and the parent and the child are in a consistent state. This event is dispatched after the container has dispatched the childAdd event and all changes that need to be made as result of the addition have happened.

=Ryan [email protected]

ryanstewart
Hi, thanks for that. Unfortunately I don't think this is the issue because I can see that the children have been created ok before I click the button that iterates through them - ie the DisplayObjects themselves are visible on the screen.Also, as the iteration doesn't happen until I click a button, I can wait for any length of time after the DisplayObjects are added before I start the iteration but even if I give it a few minutes from when I can see the DisplayObjects on the screen, I still get the same problem
Addsy