8 Replies Latest reply on Feb 3, 2009 1:01 AM by mbskels

    MXML attributes accessed via AS3

    mbskels
      Hi,

      I have a Delphi background and am struggling to learn Flex.
      Understand that MXML and AS3 are effectively equivalent and that MXML is translated into AS3.
      However I do not seem to be able to access the same properties, events in AS3 as I can access in MXML.
      For example a Button component in MXML - I can access properties and events (horizontalCenter, verticalCentre, click event) which I do not seem to be able to access in a button object instantiated in AS3.

      Could anyone confirm that this difference is real or am I missing something OR how can I access these properties, events in AS3?



        • 1. MXML attributes accessed via AS3
          jmcmichael
          Hi Malcom,

          What you want to do is give your Button component an id parameter. Then you can address that button by that id from AS3.

          Check out this section in the Flex 3 Developer's Guide for the complete details.

          Josh

          EDIT: Just realized on re-reading your question that it's a bit more in depth than that. Do you have a code sample?
          • 2. Re: MXML attributes accessed via AS3
            Level 7

            "MalcolmSkels" <webforumsuser@macromedia.com> wrote in message
            news:gm4ein$7bd$1@forums.macromedia.com...
            > Hi,
            >
            > I have a Delphi background and am struggling to learn Flex.
            > Understand that MXML and AS3 are effectively equivalent and that MXML is
            > translated into AS3.
            > However I do not seem to be able to access the same properties, events in
            > AS3
            > as I can access in MXML.
            > For example a Button component in MXML - I can access properties and
            > events
            > (horizontalCenter, verticalCentre, click event) which I do not seem to be
            > able
            > to access in a button object instantiated in AS3.
            >
            > Could anyone confirm that this difference is real or am I missing
            > something OR
            > how can I access these properties, events in AS3?

            If you see something that looks like a property in MXML and you're having
            trouble accessing itn AS, check the docs in the language reference and look
            for it in the styles.


            • 3. Re: MXML attributes accessed via AS3
              Gregory Lafrance Level 6
              In MXML, properties and style properties are accessed like properties.

              In ActionScript, properties are accessed as properties, but style properties are accessed using the getStyle and setStyle methods.

              As Amy said, always check the API in the help sys to see if something is a style property if you are having problems.
              • 4. Re: MXML attributes accessed via AS3
                mbskels Level 1
                Hi,

                Thanks for the replies – and patience for such a basic query – which has helped clarify things. It was mainly the Style issue that was confusing me. I think I have got it now but would appreciate if someone could confirm that I am correct in the following:

                <mx:Button label=”Test It” click=”myButtonClickHandler()” horizontalCenter=”0” VerticalCenter=”0” />

                1.
                In AS3 you cannot access any events of an object other than by adding an event listener with the addEventListener method. So although there is a click event for a Button which you can access in MXML as in the example above there is no direct equivalent in AS3 – you have to add a listener. In fact, this is what the MXML translation does - in AS3 you have to do it yourself.

                As you have both said a lot of properties are Styles and can only be accessed in AS3 via the SetStyle method which adds up to a lot of work just to set a few properties.
                Overall I am concluding that AS3 is pretty labour intensive and that I should use MXML if at all possible and only drop into AS3 if absolutely necessary. Is that correct? (Not intuitive for me)

                Thanks again for the assist.

                Malcolm

                • 5. Re: MXML attributes accessed via AS3
                  Level 7

                  "MalcolmSkels" <webforumsuser@macromedia.com> wrote in message
                  news:gm768m$jqr$1@forums.macromedia.com...
                  > Hi,
                  >
                  > Thanks for the replies ? and patience for such a basic query ? which has
                  > helped clarify things. It was mainly the Style issue that was confusing
                  > me. I
                  > think I have got it now but would appreciate if someone could confirm that
                  > I am
                  > correct in the following:
                  >
                  > <mx:Button label=?Test It? click=?myButtonClickHandler()?
                  > horizontalCenter=?0?
                  > VerticalCenter=?0? />
                  >
                  > 1.
                  > In AS3 you cannot access any events of an object other than by adding an
                  > event
                  > listener with the addEventListener method. So although there is a click
                  > event
                  > for a Button which you can access in MXML as in the example above there
                  > is no
                  > direct equivalent in AS3 ? you have to add a listener. In fact, this is
                  > what
                  > the MXML translation does - in AS3 you have to do it yourself.
                  >
                  > As you have both said a lot of properties are Styles and can only be
                  > accessed
                  > in AS3 via the SetStyle method which adds up to a lot of work just to set
                  > a few
                  > properties.
                  > Overall I am concluding that AS3 is pretty labour intensive and that I
                  > should
                  > use MXML if at all possible and only drop into AS3 if absolutely
                  > necessary. Is
                  > that correct? (Not intuitive for me)

                  If you're setting several properties at once, create an object with all the
                  properties and use setStyle to set that or just set the styleName to a style
                  you've set up in advance.

                  MPO is that AS3 is more intuitive, because most of the real fine tuning
                  you'll need to do once you get past the roughing in will only be documented
                  in the API, and I find that to use it in MXML you often need to do a lot of
                  translating from the API documentation into how it works in MXML, whereas it
                  pretty much works in as like it's documented in the API (most of the time).


                  • 6. Re: MXML attributes accessed via AS3
                    Gregory Lafrance Level 6
                    Using the Button "click" property is almost the same as using addEventListener in ActionScript. I say "almost" because there are things you can do in AS you can't do in MXML, such as removing the event listener.

                    Once you get used to AS, its not more difficult to use than MXML, but then you have to decide when to set properties and styles in MXML and when to set them in AS.

                    For me, if a property or style can just be set once and generally won't change at runtime, or if it must be set initially, I usually go with MXML.

                    But if it will change, in most cases I use AS.

                    Over time this WILL become intuitive, and AS is where the real power (and thus the greater expertise, which eventually can lead to greater income), so hang tough and you'll get it.
                    • 7. Re: MXML attributes accessed via AS3
                      mbskels Level 1
                      Ok. Will go with your advice. Thanks both.
                      • 8. MXML attributes accessed via AS3
                        mbskels Level 1
                        duplicate post