17 Replies Latest reply on Feb 16, 2010 12:32 AM by Scott R. Hamilton

    Using addChild() in an ActionScript vs. in a Class

    Scott R. Hamilton Level 1

      I have defined  ActionScript Class by adding all of the script code  from an AS script file

      (plus a standard constructor method).

       

      This was working as a Script but the Class file will not compile. I'm getting not defined

      errors for addChild() and removeChild().

        • 2. Re: Using addChild() in an ActionScript vs. in a Class
          Gregory Lafrance Level 6

          If you just "include" the .as script somewhere else in your code, if the container where the code was "included" supports the addChild() and removeChild() methods, you are all set, as you have seen.

           

          But if you are using this code in a class, the class must support the addChild() and removeChild() methods.

           

          Does your class extend at least UIComponent, or some other class that cupports the addChild() and removeChild() methods, such as VBox, etc.

           

          If you are extending no other class, then that's your problem, and simply extending UIComponent should fix the issue:

           

          import mx.core.UIComponent;

           

          public class MyClass extends UIComponent{

              ...

          }

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          • 3. Re: Using addChild() in an ActionScript vs. in a Class
            Scott R. Hamilton Level 1

            Here's tje cpde.

             

            The compiler if flagging these lines:

             

              addChild(formPrintView);

              removeChild(formPrintView);

              formPrintView.imPrtPict.source = fileReference.data;

             

            AddChild, RemoveChild and fileReference as not defined

             

             

            package
            {
                public class PrintRecipeGlobal
                {
              

             

                    public function PrintRecipeGlobal()
                    {
                    }

             

             

             

                    //                     _______
                    static public function doPrint(sTitle:String, sSubTitle:String, sCreator:String, sSource:String,
                                            sOrigin:String, sIntro:String, sPrepTime:String, sCookTime:String,
                                            sServings:String, sIngr:String, sPrep:String, sImagePath:String):void {

             

                    import mx.controls.Alert;
                    import mx.printing.FlexPrintJob;                  
                    import flash.display.*;            
                    import flash.events.Event;
                    import flash.net.URLRequest;  
                    import flash.display.Sprite;
                    import flash.display.Stage;
                    import flash.text.*;
                    import flash.events.*;
                    import mx.core.UIComponent;

             


                                                        
                    
                        //Alert.show("Printing called");
                       
                        // Create a FlexPrintJob instance.
                        var printJob:FlexPrintJob = new FlexPrintJob();
                   
                        // Start the print job.
                        if(printJob.start()) {
                            // Create a MyPrintView control as a child
                            // of the current view.
                            var formPrintView:PrtViewCreateScr = new PrtViewCreateScr();
                            addChild(formPrintView);
                   
                            // Populate the print control's structures with the text from
                            //the create recipe screen
                            formPrintView.lblTitle.text = sTitle;
                            formPrintView.lblSubTitle.text = sSubTitle;
                            formPrintView.lblCreator.text = sCreator;
                            formPrintView.lblSource.text = sSource;
                            formPrintView.lblOrigin.text = sOrigin;
                            formPrintView.taPrtIntro.text = sIntro;
                            formPrintView.lblPrepTime.text = sPrepTime;
                            formPrintView.lblCookTime.text = sCookTime;
                            formPrintView.lblServings.text = sServings;
                            formPrintView.taPrtIngrList.text = sIngr;
                            formPrintView.taPrtPreparation.text = sPrep;
                           
                            //Alert.show("Image Path: " + "file///" + sImagePath);
                            //formPrintView.imPrtPict.load(sImagePath);   
                            formPrintView.imPrtPict.source = fileReference.data;   
                           
                            if (sImagePath != "") {
                                var pictLdr:Loader = new Loader();
                                var pictURLReq:URLRequest = new URLRequest("file:///" + sImagePath);
                                pictLdr.load(pictURLReq);
                                pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);                        
                            }
                           
                            // Add the SimplePrintview control to the print job.
                            // For comparison, try setting the
                            // second parameter to "none".
                            printJob.addObject(formPrintView);
                   
                            // Send the job to the printer.
                            printJob.send();
                   
                            // Remove the print-specific control to free memory.
                            removeChild(formPrintView);
                        }   
                       
                        function imgLoaded(event:Event):void
                            {                   
                                Alert.show("Image load complete");
                                formPrintView.imPrtPict.load(sImagePath);   
                                //formPrintView.imPrtPict.source = sImagePath;
                            }
                    }
                    // function doPrint

             

                }
                // class PrintRecipeGlobal
            }

            • 4. Re: Using addChild() in an ActionScript vs. in a Class
              Gregory Lafrance Level 6

              Like I said, try this:

               

              public class PrintRecipeGlobal extends UIComponent

               

              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services

              • 5. Re: Using addChild() in an ActionScript vs. in a Class
                Scott R. Hamilton Level 1

                This is what I've done (thanks for the idea Greg):

                 

                public class PrintRecipeGlobal extends UIComponent
                    {

                 

                   [class code]

                 

                 

                   }

                 

                The original errors are now gone (addChild(), removeChild() and FileReference not defined)

                 

                What I'm now getting is:

                 

                The definition of the base class UIComponent could not be found.

                 

                How can this be? The errors being generated by not incorporating the base

                class UIComponent are gone but the base class itself cannot be found? So

                where  is the compiler getting the definitions for addChild(), removeChild() and

                FileReference?

                 

                Very confusing!

                 

                Scott

                • 6. Re: Using addChild() in an ActionScript vs. in a Class
                  Scott R. Hamilton Level 1

                  So what do I have to do to get the flex compiler to recognize the UIComponent base class?

                  • 7. Re: Using addChild() in an ActionScript vs. in a Class
                    Jason Szeto Level 3

                    Scott,

                     

                    You need to import mx.core.UIComponent.

                     

                    A handy trick I use in FlashBuilder is to put your caret at the end of the class and type "Ctrl-SPACE". It will automatically insert the import statement for you.

                     

                    Jason

                    • 8. Re: Using addChild() in an ActionScript vs. in a Class
                      Scott R. Hamilton Level 1

                      Thanks for your tip Jason,

                       

                      unfortunately, the problem just pops up again somewhere else.

                       

                      If I put the import statment between the package and class definitions like so:

                       

                      package
                      {

                       

                          import mx.core.UIComponent;

                       

                         
                          public class PrintRecipeGlobal extends UIComponent
                          {

                       

                       

                      I get undefined messages on compile for addChild, removeChild and fileReference again.

                       

                      If I move the import into the class definition like this:

                       

                      package
                      {

                       

                       

                       

                         
                          public class PrintRecipeGlobal extends UIComponent
                          {
                        
                              import mx.core.UIComponent;

                       

                       

                       

                              public function PrintRecipeGlobal()
                              {
                              }

                       

                      I get "... base class definition for UIComponent not found".

                       

                      I'm at my wits end here...there must be something of a very bassic nature that is

                      wrong.

                       

                      Where can I find out exactly what imports I need in order to create a class in ActionScript?

                       

                      Scott

                      • 9. Re: Using addChild() in an ActionScript vs. in a Class
                        Gregory Lafrance Level 6

                        This code works. How does your code differ (in relevant ways)?

                         

                        package
                        {
                          import mx.controls.Button;
                          import mx.core.UIComponent;
                          public class PrintRecipeGlobal extends UIComponent{
                            public function PrintRecipeGlobal(){
                              var btn:Button = new Button();
                              this.addChild(btn);
                            }
                          }
                        }
                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                          creationComplete="init()">
                          <mx:Script>
                            <![CDATA[
                              private function init():void{
                                var prg:PrintRecipeGlobal = new PrintRecipeGlobal();
                                this.addChild(prg);
                              }
                            ]]>
                          </mx:Script>
                        </mx:Application>
                        
                        


                        If this post answers your question or helps, please mark it as such.


                        Greg Lafrance - Flex 2 and 3 ACE certified

                        www.ChikaraDev.com

                        Flex / AIR Development, Training, and Support Services

                        • 10. Re: Using addChild() in an ActionScript vs. in a Class
                          Scott R. Hamilton Level 1

                          Hi Greg,

                           

                          I've spent some time reducing this down to a base minimum but I

                          still get the same error (base class component not found):

                           

                          package

                           

                           

                           

                              import flash.display.*;
                              import flash.events.Event;
                              import flash.net.URLRequest;
                              import flash.text.*;
                             
                              import mx.controls.Alert;
                              import mx.core.UIComponentDescriptor;
                              import mx.printing.FlexPrintJob;
                             
                             
                              public class PrintRecipeGlobal extends UIComponent
                              {
                                  
                                  public function PrintRecipeGlobal()
                                  {
                                      super();
                                  }   

                           

                                  //                           _______
                                  static public function doPrint(sTitle:String, sSubTitle:String, sCreator:String, sSource:String,
                                                          sOrigin:String, sIntro:String, sPrepTime:String, sCookTime:String,
                                                          sServings:String, sIngr:String, sPrep:String, sImagePath:String):void {

                           

                                                        
                                  
                                      //Alert.show("Printing called");
                                     
                                      // Create a FlexPrintJob instance.
                                      var printJob:FlexPrintJob = new FlexPrintJob();
                                 
                                      // Start the print job.
                                      if(printJob.start()) {
                                          // Create a MyPrintView control as a child
                                          // of the current view.
                                          var formPrintView:PrtViewCreateScr = new PrtViewCreateScr();
                                          addChild(formPrintView);
                                 
                                          // Populate the print control's structures with the text from
                                          //the create recipe screen
                                          formPrintView.lblTitle.text = sTitle;
                                          formPrintView.lblSubTitle.text = sSubTitle;
                                          formPrintView.lblCreator.text = sCreator;
                                          formPrintView.lblSource.text = sSource;
                                          formPrintView.lblOrigin.text = sOrigin;
                                          formPrintView.taPrtIntro.text = sIntro;
                                          formPrintView.lblPrepTime.text = sPrepTime;
                                          formPrintView.lblCookTime.text = sCookTime;
                                          formPrintView.lblServings.text = sServings;
                                          formPrintView.taPrtIngrList.text = sIngr;
                                          formPrintView.taPrtPreparation.text = sPrep;
                             
                                          formPrintView.imPrtPict.source = fileReference.data;   
                                         
                                          // Add the SimplePrintview control to the print job.
                                          printJob.addObject(formPrintView);
                                 
                                          // Send the job to the printer.
                                          printJob.send();
                                 
                                          // Remove the print-specific control to free memory.
                                          removeChild(formPrintView);
                                      }   
                                     
                                      function imgLoaded(event:Event):void
                                          {                   
                                              Alert.show("Image load complete");
                                              formPrintView.imPrtPict.load(sImagePath);   
                                              //formPrintView.imPrtPict.source = sImagePath;
                                          }
                                  }
                                  // function doPrint

                           

                              }
                              // class PrintRecipeGlobal
                          }

                          • 11. Re: Using addChild() in an ActionScript vs. in a Class
                            Scott R. Hamilton Level 1

                            Hi Greg,

                             

                            I tried your example  -  the mxml and the class.

                             

                            The result is that there are no compile errors, but the button

                            defined does not appear on the screen, which may or may not

                            be an issue.

                             

                            I guess that what's important is that the class can be successfully

                            defined as an extension of UIComponent.

                             

                            What I will try next is to isolate the class definition  and mxml

                            to see if this will work. I will define the two in a separate project

                            to see if collateral effects in my original project were playing

                            a role.

                             

                             

                            Regards,

                             

                            Scott

                            • 12. Re: Using addChild() in an ActionScript vs. in a Class
                              Gregory Lafrance Level 6

                              In your latest posted code, you don't have a line import mx.core.UIComponent.

                               

                              Flex Builder is strange sometimes in that it sometimes removed necessary import statements automatically (and erroneously)! If you get errors, always check your imports to ensure Flex Builder did not screw you.

                               

                              Here is your latest posted code, amended and working (with code I don't have commented out):

                               

                              package
                              {
                               
                               
                               
                                  import flash.display.*;
                                  import flash.events.Event;
                                  import flash.net.URLRequest;
                                  import flash.text.*;
                                  
                                  import mx.controls.Alert;
                                  import mx.core.UIComponentDescriptor;
                                  import mx.printing.FlexPrintJob;
                                  import mx.core.UIComponent;
                                  
                                  
                                  public class PrintRecipeGlobal extends UIComponent
                                  {
                                       
                                      public function PrintRecipeGlobal()
                                      {
                                          super();
                                      }    
                               
                                      //                           _______
                                      static public function doPrint(sTitle:String, sSubTitle:String, sCreator:String, sSource:String,
                                                              sOrigin:String, sIntro:String, sPrepTime:String, sCookTime:String, 
                                                              sServings:String, sIngr:String, sPrep:String, sImagePath:String):void {
                               
                                                             
                                       
                                          //Alert.show("Printing called");
                                          
                                          // Create a FlexPrintJob instance.
                                          var printJob:FlexPrintJob = new FlexPrintJob();
                                      
                                          // Start the print job.
                                          if(printJob.start()) {
                                              // Create a MyPrintView control as a child 
                                              // of the current view.
                              /*
                                              var formPrintView:PrtViewCreateScr = new PrtViewCreateScr();
                                              addChild(formPrintView);
                                      
                                              // Populate the print control's structures with the text from 
                                              //the create recipe screen
                                              formPrintView.lblTitle.text = sTitle;
                                              formPrintView.lblSubTitle.text = sSubTitle;
                                              formPrintView.lblCreator.text = sCreator; 
                                              formPrintView.lblSource.text = sSource;
                                              formPrintView.lblOrigin.text = sOrigin; 
                                              formPrintView.taPrtIntro.text = sIntro; 
                                              formPrintView.lblPrepTime.text = sPrepTime; 
                                              formPrintView.lblCookTime.text = sCookTime;
                                              formPrintView.lblServings.text = sServings;
                                              formPrintView.taPrtIngrList.text = sIngr;
                                              formPrintView.taPrtPreparation.text = sPrep;
                                  
                                              formPrintView.imPrtPict.source = fileReference.data;    
                                              
                                              // Add the SimplePrintview control to the print job.
                                              printJob.addObject(formPrintView);
                                      
                                              // Send the job to the printer.
                                              printJob.send();
                                      
                                              // Remove the print-specific control to free memory.
                                              removeChild(formPrintView);
                              */                
                                          }    
                                          
                                          function imgLoaded(event:Event):void
                                              {                    
                                                  Alert.show("Image load complete");
                              //                    formPrintView.imPrtPict.load(sImagePath);    
                                                  //formPrintView.imPrtPict.source = sImagePath;
                                              }
                                      }
                                      // function doPrint
                               
                                  }
                                  // class PrintRecipeGlobal
                              }
                              

                               

                              If this post answers your question or helps, please mark it as such.


                              Greg Lafrance - Flex 2 and 3 ACE certified

                              www.ChikaraDev.com

                              Flex / AIR Development, Training, and Support Services

                              • 13. Re: Using addChild() in an ActionScript vs. in a Class
                                Scott R. Hamilton Level 1

                                Hi Greg,

                                 

                                and herein lies the dilemma - if I put import mx.core.UIComponent

                                into the package section before the constructor I'm back where I

                                started: undefined messages for addCHild, removeChild and

                                fileReference at compile time.

                                 

                                 

                                Scott

                                • 14. Re: Using addChild() in an ActionScript vs. in a Class
                                  Flex harUI Adobe Employee

                                  You can't call addChild in a static method.  A Class's static methods and

                                  properties do not belong to any instance, and only instances can have

                                  children.  You can pass an instance into a static methods and call addChild

                                  on that instance if you want.

                                  • 15. Re: Using addChild() in an ActionScript vs. in a Class
                                    Scott R. Hamilton Level 1

                                    Hi Aley,

                                     

                                    thanks for the replay.

                                     

                                    I removed the static keyword and that cleared up all of the problems except one.

                                     

                                    The compiler still flags fileReference as undefined.

                                     

                                    Mind you, this script works as a normal ActionScript object.

                                     

                                    Scott

                                    • 16. Re: Using addChild() in an ActionScript vs. in a Class
                                      Flex harUI Adobe Employee

                                      I don't see it defined in your code.  You'll probably need to import

                                      FileReference and make an instance variable called fileReference.

                                      • 17. Re: Using addChild() in an ActionScript vs. in a Class
                                        Scott R. Hamilton Level 1

                                        Thanks Alex,

                                         

                                        the class compiles now.

                                         

                                        I've been experimenting around so much that I inadvertently removed one of the most

                                        important Imports.

                                         

                                        Scott