9 Replies Latest reply on Apr 27, 2009 4:03 PM by Gregory Lafrance

    External function

    Seglespaan

      Hi,

       

      I'm fairly new to flex and am having difficulty in accessing external functions.

       


      Where's the best place to store an external function, .as file etc?

       

      and how can my application and or custom components then access this function?

       

      Cheers

        • 1. Re: External function
          ntsiii Level 3

          Specifically what do you mean by "external"?

           

          What are you trying to accomplish?

           

          Tracy Spratt

          • 2. Re: External function
            Seglespaan Level 1

            Well as I'm new to flex I've essentially created a massive 2000 line application, and while it works before I go any further I want to break up my code a bit.

             

            the top 800 line of code is basically lots of different functions that make the application work, but i want to organise these into text functions, graphics functions etc and store them in seperate files which can then be accessed / used by the main application,

             

            so by external I mean saved outside of teh main mxml application.

             

            Cheers

             

            Tim

            • 3. Re: External function
              run,ryan! Level 3

              Yes, *.as. Action script is oo language, most thing is object ralated, you create a new instance of your custom class and call the public function belong to this class. Some time you just want a util function, then you can create a static class and call the function within this class just by YourStaticClass.yourFunction()

              HTH

              • 5. Re: External function
                Seglespaan Level 1

                ok then I suppose what I'm asking then is how do I turn this

                 

                            public var ohNew:ObjectHandles;
                            public var taNew:TextArea;           
                           
                             public function addObjectHandle(a):void
                              {
                                                
                                ohNew = new ObjectHandles();
                                ohNew.id = a;
                                ohNew.name = a;
                                ohNew.width = 300;                          
                                ohNew.height = 100;
                                ohNew.setStyle("backgroundAlpha","0"); 
                               
                               
                                taNew = new TextArea();
                                taNew.id = "ta1";
                                taNew.name = "ta1";
                                taNew.width = 100;
                                taNew.height = 100;
                                taNew.text = test.text;
                                taNew.setStyle("backgroundColor","yellow");
                           
                                ohNew.addChild(taNew);
                               
                               
                                userDesignPanel.addChild(ohNew);
                               
                              }//       

                 

                 

                into a class and then how would I use it?

                • 6. Re: External function
                  Gregory Lafrance Level 6

                  Here is your class, but I don't know about userDesignPanel and ObjectHandles. You can create an instance of this class in another file like this:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                    <mx:Script>
                      <![CDATA[
                        import com.myCompany.classes.MyClass;
                       
                        private var mc:MyClass = new MyClass();
                      ]]>
                    </mx:Script>
                  </mx:Application>

                   

                  package
                  {
                    public class MyClass{
                      public var ohNew:ObjectHandles;
                      public var taNew:TextArea;          

                      public function MyClass()
                      {
                      }
                      public function addObjectHandle(a):void{               
                        ohNew = new ObjectHandles();
                        ohNew.id = a;
                        ohNew.name = a;
                        ohNew.width = 300;                          
                        ohNew.height = 100;
                        ohNew.setStyle("backgroundAlpha","0");

                        taNew = new TextArea();
                        taNew.id = "ta1";
                        taNew.name = "ta1";
                        taNew.width = 100;
                        taNew.height = 100;
                        taNew.text = test.text;
                        taNew.setStyle("backgroundColor","yellow");
                        ohNew.addChild(taNew);
                        userDesignPanel.addChild(ohNew);
                      }       
                    }
                  }

                  • 7. Re: External function
                    Seglespaan Level 1

                    userDesignPanel is a canvas in my main app, and as you expected this is causing problems as th class doesn't know what this is. same goes for test.text

                     

                    how do I get around this?

                    • 8. Re: External function
                      Seglespaan Level 1

                      i just imported the objectHandles into the class, is that the right way to do it?

                      • 9. Re: External function
                        Gregory Lafrance Level 6

                        I need to clarify a little.

                         

                        You are adding the TextArea to the ObjectHandles object, and then are adding the ObjectHandles object to the userDesignPanel (Canvas) object.

                         

                        That's fine. If userDesignPanel is in the main app, you probably want to just add the MyClass instance to userDesignPanel.

                         

                        In that case declare MyClass as follows:

                         

                        public class MyClass extends UIComponent{

                            ...

                        }

                         

                        And then in the main app:

                         

                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                          creationComplete="init();">
                          <mx:Script>
                            <![CDATA[
                              import com.myCompany.classes.MyClass;

                         

                              private var mc:MyClass;

                         

                              private function init():void{

                                mc = new MyClass();

                                this.addChild(mc);

                              }
                            ]]>
                          </mx:Script>
                        </mx:Application>