26 Replies Latest reply on Apr 23, 2008 3:56 PM by levancho

    Included AS file cant access MXML varriables?

    jfizer Level 1
      I must be missing something here, but when I add in an Action Script file using the <script source="xxx.as"/> or include "xxx.as"; methods the AS file cant access variables I declare in the MXML file. Any ideas as to what I'm doing wrong?
        • 1. Re: Included AS file cant access MXML varriables?
          miaomm
          I don't answers just what happend with me. In one mxml the same include file works while I save it as another application then all the parameters can not be seen from the include as file. I can only say it do happen.
          • 2. Re: Included AS file cant access MXML varriables?
            jfizer Level 1
            For example, the following will generate a "Access of undefined property myString" error from the AS file.

            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationPolicy="all">
            <mx:Script>
            <![CDATA[
            public var myString:String = "Bacon";
            include "myAS.as";
            ]]>
            </mx:Script>
            </mx:Application>
            ------------------------------------------------------------------------------------------ -----------------------
            // myAS.as
            myString="Swine flesh";
            • 3. Re: Included AS file cant access MXML varriables?
              jfizer Level 1
              In addition, import directives in the MXML don't make it to the Action Script file.
              • 4. Re: Included AS file cant access MXML varriables?
                Michael Borbor Level 4
                Try using import instead of include or use this
                • 5. Included AS file cant access MXML varriables?
                  jfizer Level 1
                  quote:

                  Originally posted by: mab_bond
                  Try using import instead of include or use this


                  Huh? Unless I'm missing something, import is for classes not source code files.

                  My current solution is to put all variables into the .as file, which while it works is rather limiting.
                  • 6. Re: Included AS file cant access MXML varriables?
                    jfizer Level 1
                    This is getting ridiculous. If I cant use include files, what good is this language? My project's over 3k lines so far, am I expected to put that all in one big text file?
                    • 7. Re: Included AS file cant access MXML varriables?
                      levancho Level 3
                      you can include as file without any problem, and also access all variables that are declared in including file, but remember you need to access variables from functions you cant just treat included as file as just one big function
                      so inside your myAS.as
                      myString="Swine flesh"; will not work
                      but
                      public function myfunction () {
                      myString="Swine flesh";
                      }

                      will work.
                      • 8. Included AS file cant access MXML varriables?
                        jfizer Level 1
                        quote:

                        Originally posted by: levancho
                        you can include as file without any problem, and also access all variables that are declared in including file, but remember you need to access variables from functions you cant just treat included as file as just one big function
                        so inside your myAS.as
                        myString="Swine flesh"; will not work
                        but
                        public function myfunction () {
                        myString="Swine flesh";
                        }

                        will work.


                        Within the same function in my .as file I can access some <MXML> tags by id, but not others. No rhyme or reason. What finaly made me snap today was trying to access the HDividedBox by ID that was the parent of the other objects I'd been working with. Even though the line before and after within the function accessed the children within the HDividedBox, I got nothing but errors when I tried to change the properties of the HDividedBox itself.

                        And no variables declared in the MXML are accessible from a function in my .as file.

                        This problem only starts to show up when my projects get large. The same code works perfectly in a smaller project. And I don't think its actually an error in the compiler. Its just being generated by Flex Builder prior to compilation. However it DOES prevent Flex Builder from building the project. Short of randomly yanking out code till it works I don't know what I should be looking for. But there is something in the Builder settings or within my project files that is causing this.

                        One thing I know for sure, its a problem with the include statement. If I take the contents of the .as file verbatim and paste them in place of the include everything works fine.

                        And before its asked, I cant share my source code nor make an example that shows off the problem.
                        • 9. Re: Included AS file cant access MXML varriables?
                          Michael Borbor Level 4
                          Watch your attitude man, nobody wants to steal your code or something like. We're here the share as a community. Your problem is essentially a Design Patter, you should learn about OOP, and how to use it. If you wanna access an external variable you should create a class and define its properties, and so on... If you wanna develop huge application you ought to read OReilly ActionScript 3.0 Design Patterns.
                          • 10. Re: Included AS file cant access MXML varriables?
                            jfizer Level 1
                            quote:

                            Originally posted by: mab_bond
                            Watch your attitude man, nobody wants to steal your code or something like. We're here the share as a community. Your problem is essentially a Design Patter, you should learn about OOP, and how to use it. If you wanna access an external variable you should create a class and define its properties, and so on... If you wanna develop huge application you ought to read OReilly ActionScript 3.0 Design Patterns.


                            Was making no such implication, just saying that I cant share my code as much as I'd like to since I'm unable to reproduce the problem on smaller projects (shrug). And I'm not trying to access an external variable. I simply want the include directive to work as its supposed to. I am using it correctly, but the results are inconstant and strange. As I've already said, within the same block of <MXML> I can access some objects by ID but not others. There is no logical explanation that I can find.

                            • 11. Included AS file cant access MXML varriables?
                              jfizer Level 1
                              Ugh, this is starting to make my head hurt.

                              So today I uncommented the line of code that last night was causing errors. No change was made, but today it works fine. This is without a doubt a problem with the Flex Builder environment rather then a problem with the source code I'm working with.
                              • 12. Re: Included AS file cant access MXML varriables?
                                slaingod Level 1
                                any time you start seeing unexplained behavior in FB it is usually a good idea to restart FB and then do a clean build of your project. I do so at least twice a day. FB/Eclipse are very far from perfect, but usually they help more than they hurt.
                                • 13. Re: Included AS file cant access MXML varriables?
                                  jfizer Level 1
                                  quote:

                                  Originally posted by: slaingod
                                  any time you start seeing unexplained behavior in FB it is usually a good idea to restart FB and then do a clean build of your project. I do so at least twice a day. FB/Eclipse are very far from perfect, but usually they help more than they hurt.


                                  I know and I did that yesterday. I really wish I could explain the behavior I'm seeing. But it's just defying all logic.

                                  However, as a backup, does anyone have a list of what parameters I would need to use to compile from the command line with the data visualization objects? Not sure of the class paths off hand.
                                  • 14. Included AS file cant access MXML varriables?
                                    levancho Level 3
                                    "I can access some objects by ID but not others. There is no logical explanation that I can find."

                                    if you want to access all objects by id withing same mxml block, than you are doing something very very wrong, basically that means you are building application where all objects are siblings and none of them have children, mxml ids are basically same as variable names, so that means you can only access mxml objects by id, if they are defined within same block of mxml, you can not access mxml objects by id if the are defined outside of the block, or even inside of the other object which is defined inside your block.

                                    IMHO, best way to help is on example, since your code carries proprietory nature, you can write a similiar bogus example that shows how you are trying to access, objects tc ... and what error you are getting ,that should not take you more than 5 monutes, that way it will be easier for guys to help you
                                    and I think it will finaly save you more frustration and angrey development days :)

                                    Kind Regards.
                                    • 15. Re: Included AS file cant access MXML varriables?
                                      jfizer Level 1
                                      Not sure I understand what your saying. Of course my MXML objects have parents and children, but when I had problems I could access the children but not the parent by ID. In this case it was because I was working on dynamicly resizing the sections of a HDividedBox and was under the miss impression that I needed to resize the children rather then setting the divider position.

                                      As for providing an example, the problem is that the issues comes and goes. So even if I could provide source code, there is no reason to expect that anyone else would see the problem with the example I could provide.
                                      • 16. Included AS file cant access MXML varriables?
                                        levancho Level 3
                                        quote:

                                        Originally posted by: jfizer
                                        Not sure I understand what your saying. Of course my MXML objects have parents and children, but when I had problems I could access the children but not the parent by ID..

                                        Object A can access its children with ids: (b,c,d) (which is same as by variable name) because they are part of that object ,so somewhere in object A there is code :
                                        b = childOne
                                        c = childTwo
                                        d = childThree
                                        thats why objct A can say c.doSomething, b.doSomething else etc ...
                                        but
                                        Object A can not access its parent (f) By id ,because
                                        Object A does not have :
                                        f = parentObject
                                        (although DisplayObject has property called "parent" which is invented for that reason so one DisplayObject can cascade up the hierarchy to its parent etc ...).
                                        • 17. Included AS file cant access MXML varriables?
                                          jfizer Level 1
                                          quote:

                                          Originally posted by: levancho
                                          quote:

                                          Originally posted by: jfizer
                                          Not sure I understand what your saying. Of course my MXML objects have parents and children, but when I had problems I could access the children but not the parent by ID..

                                          Object A can access its children with ids: (b,c,d) (which is same as by variable name) because they are part of that object ,so somewhere in object A there is code :
                                          b = childOne
                                          c = childTwo
                                          d = childThree
                                          thats why objct A can say c.doSomething, b.doSomething else etc ...
                                          but
                                          Object A can not access its parent (f) By id ,because
                                          Object A does not have :
                                          f = parentObject
                                          (although DisplayObject has property called "parent" which is invented for that reason so one DisplayObject can cascade up the hierarchy to its parent etc ...).



                                          I understand all of that, but I fail to see what it has to do with what I'm talking about where actionscript outside of the parent child hierarchy of the MXML objects cant access items by ID or even AS variables defined elsewhere. I have for example MXML as follows...

                                          <mx:HDividedBox id="daBox" width="100%" height="100%">
                                          <mx:Canvas id="daCanvas" label="Canvas 1" width="100%" height="100%" backgroundColor="#FFFFCC">
                                          <mx:Label id="daText" text="Add components here" fontWeight="bold"/>
                                          </mx:Canvas>
                                          <mx:Canvas label="Canvas 2" width="100%" height="100%" backgroundColor="#99CCFF">
                                          <mx:Label text="Add components here" fontWeight="bold"/>
                                          </mx:Canvas>
                                          </mx:HDividedBox>

                                          Now my action script code could change the properties of daCanvas or daText but not daBox. At least until it magically started working this morning (well yesterday morning by now). At a guess, this is a problem with the syntax checker, but because its intermittent its hard to really check.
                                          • 18. Re: Included AS file cant access MXML varriables?
                                            levancho Level 3
                                            <mx:HDividedBox id="daBox" width="100%" height="100%">
                                            <mx:Canvas id="daCanvas" label="Canvas 1" width="100%" height="100%" backgroundColor="#FFFFCC">
                                            <mx:Label id="daText" text="Add components here" fontWeight="bold"/>
                                            </mx:Canvas>
                                            <mx:Canvas label="Canvas 2" width="100%" height="100%" backgroundColor="#99CCFF">
                                            <mx:Label text="Add components here" fontWeight="bold"/>
                                            </mx:Canvas>
                                            </mx:HDividedBox>

                                            ----------

                                            IT IS NOT POSSIBLE to access daCanvas and daText by just their Id from same function of same included AS file eitheryou can not access them at all, or can access one or the other, not both.

                                            I suggest you read developer Guide, at :
                                            http://www.adobe.com/support/documentation/en/flex/
                                            it will make it clear.
                                            • 19. Re: Included AS file cant access MXML varriables?
                                              Michael Borbor Level 4
                                              Levancho why do you say that isn't possible to access daCanvas and daText?
                                              • 20. Re: Included AS file cant access MXML varriables?
                                                jfizer Level 1
                                                quote:

                                                IT IS NOT POSSIBLE

                                                Huh? Of course its possible. People do it all the time. Nothing in the documentation says you cant and common sense says you can. In fact, I'm doing it right now.
                                                • 21. Re: Included AS file cant access MXML varriables?
                                                  Michael Borbor Level 4
                                                  Of course it's posibble. When I need to refer a control, container or whatever I use Application.application to have access to everything.
                                                  • 22. Included AS file cant access MXML varriables?
                                                    levancho Level 3
                                                    quote:

                                                    Originally posted by: mab_bond
                                                    Levancho why do you say that isn't possible to access daCanvas and daText?


                                                    because daText is child of canvas you need first get reference to the canvas and then get daText.

                                                    run below example and you will see :
                                                    main app :
                                                    <?xml version="1.0" encoding="utf-8"?>
                                                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

                                                    <mx:Script>
                                                    <![CDATA[




                                                    private function test2 (): void {
                                                    trace(this.daPanel);
                                                    trace(this.daText);// Access to possibly undefined method.
                                                    }

                                                    ]]>
                                                    </mx:Script>
                                                    <local:testPanel id="daPanel" />
                                                    </mx:Application>

                                                    ---- testPanel.mxml
                                                    <?xml version="1.0" encoding="utf-8"?>
                                                    <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300">
                                                    <mx:Text id= "daText" x="64" y="95" text="Text" />
                                                    </mx:Canvas>

                                                    • 23. Re: Included AS file cant access MXML varriables?
                                                      Michael Borbor Level 4
                                                      Nop, there you're wrong in a matter of layout it's a child but in as you can reference it directly, or through the canvas itself.
                                                      • 24. Included AS file cant access MXML varriables?
                                                        levancho Level 3
                                                        run below example and you will see :
                                                        quote:

                                                        Originally posted by: levancho

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

                                                        <mx:Script>
                                                        <![CDATA[




                                                        private function test2 (): void {
                                                        trace(this.daPanel);
                                                        trace(this.daText);// Access to possibly undefined method.
                                                        }

                                                        ]]>
                                                        </mx:Script>
                                                        <local:testPanel id="daPanel" />
                                                        </mx:Application>

                                                        ---- testPanel.mxml
                                                        <?xml version="1.0" encoding="utf-8"?>
                                                        <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400" height="300">
                                                        <mx:Text id= "daText" x="64" y="95" text="Text" />
                                                        </mx:Canvas>




                                                        • 25. Re: Included AS file cant access MXML varriables?
                                                          Michael Borbor Level 4
                                                          <?xml version="1.0" encoding="utf-8"?>
                                                          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" >
                                                          <mx:Script>
                                                          private function test():void{
                                                          trace(this.daCanv.label);
                                                          trace(this.daText.text);
                                                          }
                                                          </mx:Script>
                                                          <mx:Canvas id="daCanv" x="124" y="74" width="200" height="200" label="Test">
                                                          <mx:Text id="daText" x="67" y="99" text="Text"/>
                                                          </mx:Canvas>
                                                          <mx:Button x="174" y="339" label="Button" click="test()"/>


                                                          </mx:Application>

                                                          Levancho try this one, and tell me what do you see on your trace window?
                                                          • 26. Re: Included AS file cant access MXML varriables?
                                                            levancho Level 3
                                                            quote:

                                                            Originally posted by: mab_bond
                                                            Levancho why do you say that isn't possible to access daCanvas and daText?


                                                            <mx:Label id="daText" text="Add components here" fontWeight="bold"/> in AS is something like this

                                                            var daText:Text = new Text() etc ....
                                                            so if you are building application with one biiiiiig MXL file then yes because all ID's will be defined in that file.so in your case yes it will work.