Addsy
Addsy

Reputation: 4054

Adding dynamic DisplayObjects in Flex

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?

Upvotes: 0

Views: 833

Answers (2)

ryanstewart
ryanstewart

Reputation: 1014

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]

Upvotes: 2

invertedSpear
invertedSpear

Reputation: 11054

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
        }
    )
}

Upvotes: 0

Related Questions