5 Replies Latest reply on Dec 15, 2009 4:10 AM by Wiouou

    How to use classes of packages in flex <mx:Script/> or <mxml/>

    Ankurwahi Level 1

      Hi.I am just learning Flex using Flex Builder 3 facing one problem,

       

      Suppose I declare one package with name alert.as

       

      package
      {
      import mx.controls.Alert;

       

      public class alert
      {
      public function alertBtn()
      {
      Alert("Hello btn 1");
      }

       

      }
      }

       

      Now in want to use the function in mxml that I declared in a package.

       

       

      <mx:Button label="btn1" click="alertBtn();"  />

       

      I have few questions

       

      1)How to Import the class alert.as in <mx:Script> and where should i store the file alert.as in the directory folder of flex?

      2)How to call the function alertBtn() when btn1 is clicked.

       

       

      Thanks so much!

      Regards

      Ankur

        • 1. Re: How to use classes of packages in flex <mx:Script/> or <mxml/>
          Gregory Lafrance Level 6

          1)How to Import the class alert.as in <mx:Script> and where should i store the file alert.as in the directory folder of flex?

           

          import alert;     if in same directory as mxml file

          import myClasses.alert;      if in directory my classes

           

          often classes are places in a directory structure lie this:

           

          com.yourCompany.classes.PURPOSE

           

          where PURPOSE might be "events" etc.

           

          2)How to call the function alertBtn() when btn1 is clicked.

           

          If your function is static, you can call it with alert.alertBtn()

           

          If not static, then create an instance

           

          private var myinstance:alert = new alert();

           

          <mx:Button label="btn1" click="myInstance.alertBtn();"  />

           

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


          Greg Lafrance
          www.ChikaraDev.com

          Flex Development and Support Services

          1 person found this helpful
          • 2. Re: How to use classes of packages in flex <mx:Script/> or <mxml/>
            Ankurwahi Level 1

            Hello Greg Lafrance

             

            Thanks for Ur Help!Your answer is helpful and I have marked it helpful!

             

            I want to ask You One more thing.Suppose there is one button

            <mx:Button id="btn1" >

             

            In a <script> tag we can access the btn1 directly like

            btn1.Label="button1";

             

            I want to use the button having id=btn1 inside the package then what should I need to do?

             

            do I need do declare the instance of btn1 like this

            var btn_1:btn1;


            ?

             

             

            Thanks

            Ankur

            • 3. Re: How to use classes of packages in flex <mx:Script/> or <mxml/>
              Gregory Lafrance Level 6

              I imagine what you really mean is you want to access the button in a class, not in a package.

               

              If an instance of the button exists in the main MXML file for example myBtn, you can access it with parentApplication.myBtn if the class where you have parentApplication.myBtn has an instance in the main MXML file (hence the main app is the parentApplication.

               

              If you just want to have an instance of a component in a class, as long as you import the component

               

              import mycomponents.mysubFolder.MyFancyComponent;

               

              then you can create an instance of the component.

               

              Not exactly clear on your situation.

               

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


              Greg Lafrance
              www.ChikaraDev.com

              Flex Development and Support Services

              • 4. Re: How to use classes of packages in flex <mx:Script/> or <mxml/>
                Ankurwahi Level 1

                 

                Hi Greg.I think I was not able to clear my problem properly.Let me try this time again.

                 

                What I wanted to do was that in the below written code I have the full access of the id=panel1 in the  script tag .This works properly.

                 

                <?xml version="1.0"?>

                 

                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

                 

                 

                <mx:Script><![CDATA[

                 

                import flash.display.Sprite;

                import mx.core.UIComponent;

                private function addChildToPanel():void {

                var circle:Sprite = new Sprite();

                circle.graphics.beginFill(0xFFCC00);

                circle.graphics.drawCircle(0, 0, 20);

                var c:UIComponent = new UIComponent();

                c.addChild(circle);

                panel1.addChild(c);

                }

                ]]></mx:Script>

                 

                <mx:Panel id="panel1" height="100" width="100"/>

                <mx:Button id="myButton" label="Click Me" click="addChildToPanel();"/>

                </mx:Application>

                 

                 

                This above functionality when I tried to do using class Outside the code ,its not working !

                **************************************************************************************

                Here is with the package:-

                 

                But suppose I make One package 

                package

                {

                import flash.display.Sprite;

                import mx.core.UIComponent;

                 

                class getPanel extends Sprite

                {

                 

                 

                public function addChildToPanel():void {

                var circle:Sprite = new Sprite();

                circle.graphics.beginFill(0xFFCC00);

                circle.graphics.drawCircle(0, 0, 20);

                var c:UIComponent = new UIComponent();

                c.addChild(circle);

                panel1.addChild(c);

                 

                     }

                 

                 

                          }//class

                 

                                    }//package

                 

                 

                Now in MXML

                 

                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

                 

                 

                <mx:Script><![CDATA[

                 

                import getPanel;

                 

                 

                ]]></mx:Script>

                 

                <mx:Panel id="panel1" height="100" width="100"/>

                <mx:Button id="myButton" label="Click Me" click="getPanel.addChildToPanel();"/>

                </mx:Application>

                 

                So My problem is that this code doesnt do anything.

                Neither the addChild function is working in it ,Nor the Panel1 is accessible here.

                 

                Can u pls help me here.

                 

                Thanks

                Ankur

                • 5. Re: How to use classes of packages in flex <mx:Script/> or <mxml/>
                  Wiouou

                  The problem is that you imported the class, meaning the compiler knows where to get the code but you don't use this code.  There is no variable of the type getPanel in your code.  Either you place a tag getPanel there with a custom namespace or you declare variable of that type in your mx:Script tag.

                   

                  But your code would still fail because you reference a variable panel1 in that method.  In your first code sample this would work because the method is known within the class (your component will be compiled into an actionscript class).  In the second sample your variable name hasn't been declared inside the class, so you should provide it one way or another.  You can do this by adding a panel1 : Panel parameter.  In this case don't forget to either instantiate a variable of type getPanel in your component or making the addChild.. method static so you can access it at class level instead of object level.