7 Replies Latest reply on Apr 20, 2009 10:33 AM by injpix

    Stuck on the simple stuff

    Mark60015 Level 1

      All-

       

      I'm working on a project and have hit a wall on dynamically generating content to be placed in a panel or vbox. Decided to boil it down to the simplest problem.

       

      Here's the issue- I have created a component in mxml which instantiates fine in mxml, but I can't seem to do the same thing in Actionscript.

       

      I get two compiler errors on the addChild() code that states "Access of undefined property instBox" and "Access of undefined property pageTwoVbox".

       

      Any idea what's going on here?

       

      Here's the code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="615" height="525" backgroundColor="#FFFFFF" backgroundGradientColors="[#FFFFFF, #FFFFFF]" backgroundGradientAlphas="[1.0, 1.0]" fontSize="14" fontFamily="Trebuchet MS" xmlns:mycomps="components.*" xmlns:mycomps1="mycomps.*">
          <mx:Panel width="613" height="395" layout="absolute" verticalScrollPolicy="on" id="pageTwoPanel">
              <mx:VBox x="0" y="0" id="pageTwoVbox" verticalGap="0" height="100%" width="100%" creationPolicy="all">
                  <mycomps1:basicBox id="compOne"/>
                  <mycomps1:basicBox id="compTwo"/>
              </mx:VBox>
          </mx:Panel>
         
          <mx:Script>
              <![CDATA[
                  import mx.containers.Panel;
                  import mx.containers.VBox;
                  import mx.containers.Box;
                  import mycomps.basicBox;
                 
                  var instBox:basicBox = new basicBox();
                  pageTwoVbox.addChild(instBox);
              ]]>
          </mx:Script>
      </mx:Application>

       

      Here's the component: (BasicBox)

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" borderColor="#C61470" backgroundColor="#C2CD19">
      </mx:Canvas>

        • 1. Re: Stuck on the simple stuff
          injpix Level 3

          The addChild() needs to be in a function.

              <mx:Script>
                  <![CDATA[
                      import mx.containers.Panel;
                      import mx.containers.VBox;
                      import mx.containers.Box;
                      import mycomps.basicBox;
                     
                      var instBox:basicBox = new basicBox();
          
                      private function init():void
                      {
                        pageTwoVbox.addChild(instBox);
                      }
                  ]]>
              </mx:Script>
          
          1 person found this helpful
          • 2. Re: Stuck on the simple stuff
            Mark60015 Level 1

            Thanks!-

             

            That solves the compiler error...

            When I modify the code and run I don't see the component inside the vBox.

             

            Do I need to do something else to the vBox to show content added to it via AS3?

            • 3. Re: Stuck on the simple stuff
              wvxvw Level 1

              In order to do it "properly" you'd need to call all the methods of the component that are called by LayoutManager and Container (validateProperies(), validateSize() etc)... but if you want to skip those, in most cases validateNow() would be sufficent.

              1 person found this helpful
              • 4. Re: Stuck on the simple stuff
                Mark60015 Level 1

                I tried the validateNow() on both the Vbox and the enclosing Panel- no luck. Tried some of the other validation methods with same result. The component doesn't show in the vbox.

                • 5. Re: Stuck on the simple stuff
                  injpix Level 3

                  I copied an paste your code into a new Flex project and when I do the fix I mentioned, I see 3 green-yellow containers inside a Panel's list.  Your component's borderColor value will not been seen until it has a value for it's borderThickness.  So assign the borderThickness to something greater then zero.  Does that help?

                  • 6. Re: Stuck on the simple stuff
                    Mark60015 Level 1

                    It seems to be working now- I had read the function and entered it and it didn't work, then after your respose I copied the code from the post and it worked fine. Must have been some bone-haed typing error on my part.

                     

                    Thanks for all your help!

                    • 7. Re: Stuck on the simple stuff
                      injpix Level 3

                      You shouldn't have to explicitly invoke validate methods, that should be left for Flex to do.  Unless, something needs to be validated before further code execution.  If you don't need to validate something immediately, then conserve the CPU and wait for the next frame, which will then call those validate methods if needed.  If you ultimately want to call a validate method for a specific property, then you can simply mark the component dirty via invalidate methods.

                       

                      Below lists invalidation methods and their corresponding validation methods:

                       

                      invalidation methods

                      validation methods
                      invalidateProperties()commitProperties()
                      invalidateSize() measure()
                      invalidateDisplayList()updateDisplayList()

                       

                       

                      So, in your custom class you would do the following:

                      1.) set value for property

                      2.) mark that property dirty

                      3.) then override the validate method and assign the value to the property.

                       

                      private var _size:Number;

                      public function set size(value:Number):void

                      {

                           //step 1

                       

                           _size = value;

                       

                          //step 2

                           invalidateSize();

                      }

                       

                      //step 3

                      override protected function measure():void

                      {

                           super.measure();

                           measuredWidth = _size;

                           measuredHeight = _size*2;

                      }  

                       

                       

                      ref. Programming Flex 2, Ch.19