3 Replies Latest reply on May 16, 2008 12:44 PM by ntsiii

    A very newby question

    _LJ
      Hi all,
      I am a new learner of Flex and action script. I just got a very simple question that, for example, if i have 3 HBox:
      <mx:HBox id="1" .../>
      <mx:HBox id="2" .../>
      <mx:HBox id="3" .../>

      Then in my action class, for example, if i use:

      var text:Text = new Text();
      ....
      this.1.addChild(text);
      this.2.addChild(text);
      this.3.addChild(text);


      Then my template wont generate, i guess there is an error or something. So if i want to create one component and registered it in 3 or more different containers, can i do this?

      If can't, then how i can just create one component and use it in multiple place instead of creating multiple components and change them one after another?


      Thanks in advance!
        • 1. Re: A very newby question
          FlexPrashant Level 1
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
          creationComplete="mytest();"
          >
          <mx:Script>
          <![CDATA[
          import mx.controls.Text;

          public var text:Text;
          public function mytest():void
          {
          text = new Text();
          text.height=40;
          text.width = 50;
          text.setStyle("borderColor","Yellow");
          text.setStyle("borderStyle","solid");
          text.setStyle("borderThickness","2");
          text.text="hello";
          h1.addChild(text);
          //h2.addChild(text);
          //h3.addChild(text);

          }



          ]]>
          </mx:Script>
          <mx:HBox x="0" width="100%" height="50" backgroundColor="red" id="h1" y="100"/>

          <mx:HBox x="0" width="100%" height="50" backgroundColor="#0000ff" id="h2" y="200"/>

          <mx:HBox x="0" width="100%" height="50" backgroundColor="#808040" id="h3" y="300"/>

          </mx:Application>


          • 2. Re: A very newby question
            theNOCer
            you could always may and array of them and do it in afor loop


            var texts:array = new array();

            for( var i:int =0; i < quantity_wanted; i++){

            var tmp:Text = new Text()
            tmp.width = 50;
            tmp.setStyle("borderColor","Yellow");
            tmp.setStyle("borderStyle","solid");
            tmp.setStyle("borderThickness","2");
            ....
            texts.push(tmp);

            }

            that will generate as many as you want


            as for making one and using it over and over again. im not sure you can do this. even if you could im not sure you would want it because i would think when you put text into one the text would appear in all.
            • 3. Re: A very newby question
              ntsiii Level 3
              Yes, a single instance of a component can only have one parent.

              You are trying to put "text" into three components. Create a new instance for each component.

              Tracy