2 Replies Latest reply on May 15, 2009 10:03 PM by Lopo Nopo

    Loading an "MXML Component" at run-time creates a null object.

    Lopo Nopo

      Hello!

       

      I have a simple Flex 3 project with an MXML application file (the parent) and an MXML component file (the child).

      At run-time I create childs of this component, and I add it to the stage using a simple "this.addChild()" call.

       

      Now, besides that, in the creation loop, after every

        newChild = new mcComp();

      I want to setup a few more custom parameters which belongs to this class. For example, now I want to set a label's text, this label is at the child.

       

      The issue is that the label isn't created until I exit the creation function, and actually, until my code returns control to the Flash Player.

       

      See my problem here?

      With custom classes which resides at .AS files, when I instantiate them with the "new" operator, they run their constructure's code and eveything is fine.

      But, when I do the same with those MXML components (which by the way are based over the Canvas class), their constructure do not execute and actually no other child of them is created.

       

      Can anyone please advise? I must be missing some keyword here.. hopefully.

        • 1. Re: Loading an "MXML Component" at run-time creates a null object.
          _Natasha_ Level 4

          Hi,

          the problem is because in AS you see all what is happened in constructor.

          When you create an object from mxml you only initiate creation process. Then component dispatch creationComplete event, only after that all child components are ready for usage.

          So there two variants if you want to set some properties:

          set if after creationComplete event

          or

          create variables (string or number and so on) in this component and bind them to inner controls (to Label's text and so on). So you can set values to variables without errors and can see them after creation automaticaly.

          • 2. Re: Loading an "MXML Component" at run-time creates a null object.
            Lopo Nopo Level 1

            Hi Natasha thanks,

             

            The issue is that the creationComplete event doesn't dispatch even when I addChild() the object.

             

            var child:mcChild = null;

            for(var i:int; i < 3; i++)

            {

                 child = new mcChild();

                 // setting some properties, labels' text etc'

                 this.addChild(child);

            }

             

            Try this and you'll see - the event doesn't dispatch until you got out of the code flow.

             

            Anyway after looking at Flex's documentation I solved it differently;

            After the addChild() call the child receives an "initialize" event. After that I could modify my label.

             

            Thank you though!