5 Replies Latest reply on Mar 28, 2008 1:51 PM by steve barker

    mxml using AS class

    steve barker
      Hi,

      I want to be able to talk to an AS class with mxml but am finding it quite difficult. I tried a few approaches:

      instantiate the class from within a <mx: Script .... and also <local:ASclassName..... and also an click event from a button but in all cases the class does not render the results. Here is a snippet


      <mx:Script>
      <![CDATA[
      function initApp():void
      {
      var basic:basicClass = new basicClass();
      }
      ]]>
      </mx:Script>

      package
      {
      import flash.display.Sprite;
      import flash.text.TextField;
      import flash.events.Event;

      public class basicClass extends Sprite
      {
      public function basicClass():void
      {
      var textBox:TextField = new TextField();
      textBox.text = "here is some text";
      addChild(textBox);
      }

      }
      }


      thanks for any help
        • 1. Re: mxml using AS class
          Gregory Lafrance Level 6
          Maybe declare the var outside the function? It seems to be of local scope to the function.
          • 2. Re: mxml using AS class
            steve barker Level 1
            Thanks for the reply Greg. I have changed the example now to try to make things easier but still I cant get it to run:

            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">

            <mx:Script>
            <![CDATA[

            import TestClassPackage.MyTestClass;
            import flash.display.Sprite;

            private var mytestclass:MyTestClass;

            private function initApp():void
            {
            var mytestclass:MyTestClass = new MyTestClass();

            addChild(mytestclass.myLabel);

            }

            ]]>
            </mx:Script>

            </mx:Application>



            here is the as code

            package TestClassPackage
            {
            import flash.display.Sprite;

            import mx.controls.Label;

            public class MyTestClass extends Sprite
            {
            public var myLabel:Label;

            public function MyTestClass()
            {

            var myLabel:Label = new Label();
            myLabel.text="here is a label";
            trace(myLabel.text);
            }

            }
            }

            thanks for any suggestions. I am sure it can be done fairly easily but its killing me trying to get the mxml app to talk to the class

            thanks again

            • 3. Re: mxml using AS class
              Garyl Woolworth Level 1
              The problem lies with the following lines.

              public var myLabel:Label;

              public function MyTestClass(){
              var myLabel:Label = new Label();
              myLabel.text="here is a label";
              trace(myLabel.text);
              }

              You are using var myLabel inside of the constructor. This creates a new scope of the variable myLabel. And the label you accessing inside of your application is referencing the original myLabel out of the constructor. The proper way to do this would be as follows.

              public function MyTestClass(){
              myLabel = new Label();
              myLabel.text="here is a label";
              trace(myLabel.text);
              }
              • 4. Re: mxml using AS class
                Bryan Dresselhaus Level 1
                Have your class extend an UIComponent subclass such as mx.containers.HBox.

                package {
                import mx.containers.HBox;
                import mx.controls.TextInput;

                public class NameInfo extends HBox {

                public var firstName:String = new String();
                public var lastName:String = new String();

                private var firstNameTextInput:TextInput = new TextInput();
                private var lastNameTextInput:TextInput = new TextInput();

                public function NameInfo(){
                super();
                }

                protected override function createChildren():void {
                super.createChildren();
                addChild(firstNameTextInput);
                addChild(lastNameTextInput);

                firstNameTextInput.text = firstName;
                lastNameTextInput.text = lastName;
                }

                }
                }


                Then you can use it like:

                <local:NameInfo firstName="John" lastName="Doe"/>

                • 5. Re: mxml using AS class
                  steve barker Level 1
                  thank you for the replies

                  both of them are very useful and answer the questions - thanks again