4 Replies Latest reply on Nov 17, 2007 6:54 AM by TeotiGraphix

    Knowing when a child has been added

    Novian
      Hi there,

      I'm adding a child via mxml e.g AddChild. I'm wondering how I go about knowing when the child has been added. I want to call a function when it has. I saw the 'added' method but I'm not quite sure how it works. Do I need to create a listener?

      Thanks.

      Novian
        • 1. Re: Knowing when a child has been added
          ntsiii Level 3
          addChild is immediate. You can call functions anytime.

          In fact, you can call functions on the child before you call addChild. The instance is created when you do new.

          Tracy
          • 2. Re: Knowing when a child has been added
            John R Nyquist Level 1
            There are the following events you could listen to:

            added
            Dispatched when a display object is added to the display list.

            addedToStage
            Dispatched when a display object is added to the on stage display list, either directly or through the addition of a sub tree in which the display object is contained.
            • 3. Re: Knowing when a child has been added
              ChristopherCesarz Level 1
              var numberOfChildren:uint = componentName.numChildren;
              or
              componentName.getChildIndex(child'sName); //If this returns -1, the child is not there.
              or
              if(componentName.getChildByName(child'sName) != null)...
              or
              if(componentName.getChildren().toString().indexOf(child'sName) != -1)...

              If you want to know for sure if the child is really in there! After checking, if true, then call a function, else, return.
              • 4. Re: Knowing when a child has been added
                TeotiGraphix Level 3
                Hi,

                If you want to know when a content child has been added, listen for;

                ChildExistenceChangedEvent.CHILD_ADDED

                or

                ChildExistenceChangedEvent.CHILD_REMOVED

                The event.releatedObject will contain the instance that fired the event. The currentTarget is the parent that just added the new child.

                The added event gets dispatched by the player and gets called for 'every' display list addition to the component, chrome and content children.

                Peace,
                Mike