A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Simple Question #4: ActionScript vs MXML

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    20

    Simple Question #4: ActionScript vs MXML

    Well, I'm making enormous progress on my first ActionScript/Flash Builder application. I've got all sorts of cool gizmos on the screen and they are beginning to interact with each other. Cool.

    But I'm wondering... Why I am using AS.

    Why am I not using MXML?

    I don't understand why there are two different languages, and when to use one vs when to use the other.

    My aesthetic sense (being a C programmer from way back) is to far prefer AS. It looks like C. In contrast, MXML looks clumsy. But I'm willing to concede that that is just a question of familiarity, and its obvious that MXML is far more concise.

    Any thoughts?

  2. #2
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    From my limited experience with other developers and exposure to flex, It's handy for creating large RIA projects where system integration is important. There are alot more components and built in tools in flex for say a database admin who needs to throw together a front end interface to display complex backend data.
    [SIGPIC][/SIGPIC]

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    20
    Interesting. Thank you for the reply.

    This gets to another question...user interface components. Is there a way to access high-level components using just ActionScript? I am a little baffled here, because this seems so elementary, but everything I have found seems to fall into one of two categories:

    1) Great components that appear only accessible via MXML code or

    2) Examples of very primitive code being written directly in AS.

    For example, I've been looking for a simple button control. I want to be able to supply PNG files for each of my button states (eg, "UP", "DOWN", "HOVER", etc). I see some powerful stuff in the new Flex libraries but that means MXML. And I've seen some pure AS stuff as well, but those are all written at a super low level...setting event handlers to capture the completion of the PNG load and such. Surely there are mature libraries of UI gadgets that are pure AS?

    (I mean, if a simple button needs to be hand coded, something like a color picker is going to be even more work. I would have thought all this is available.)

    I must be missing something here. Any advice or pointers would be most appreciated!!

    Cheers!

  4. #4
    Member
    Join Date
    May 2012
    Posts
    51
    I don't think there are many people who develop real-world projects using AS3 alone. Flash is not Java. It's not a general purpose programming platform. ActionScript exists to supplement Flash's core abilities. If you're trying to do things in a "pure-AS" matter you're probably going up the wrong street.

    Typically, one would create the visuals of an app in Flash Professional. Then behaviors are attached to the objects on the stage via ActionScript.

    Here's what an applet might look like:

    Actionscript Code:
    package
    {
        import fl.controls.ColorPicker;
        import fl.events.ColorPickerEvent;
       
        import flash.display.SimpleButton;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.net.FileReference;
       
        public class Editor extends Sprite
        {
            public var saveButton:SimpleButton;
            public var openButton:SimpleButton;
            public var colorPicker:ColorPicker;    
            public var photo:Sprite;
           
            public function Editor() {
                saveButton.addEventListener(MouseEvent.CLICK, onSaveButtonClick, false, 0, true);
                openButton.addEventListener(MouseEvent.CLICK, onOpenButtonClick, false, 0, true);
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, onColorPickerChange, false, 0, true);
            }
           
            protected function onSaveButtonClick(e:MouseEvent):void {
                var fileRef:FileReference = new FileReference;
                fileRef.save('Hello world', 'something.txt');
            }
           
            protected function onOpenButtonClick(e:MouseEvent):void {
                var fileRef:FileReference = new FileReference;
                fileRef.browse();
            }
           
            protected function onColorPickerChange(e:ColorPickerEvent):void {
                photo.opaqueBackground = e.color;
            }
        }
    }

    This app doesn't do much. It just opens up a file selection selection dialog box when the Save or Open button is clicked. I also tossed in a color picker since that's what you're looking for. All it does is change the background color of a photo.

    Looking at the code, you might wonder: where are the UI components coming from? They're declared in the class, but never created. The answer: Flash Professional will create them for you. Here're the source files:

    https://dl.dropbox.com/u/80776822/Editor.zip

    If you haven't installed Flash Professional, it's probably time to do so. Should check out some tutorials as well, especial on how to use the FP + FB combo.

  5. #5
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    I usually don't answer these type of questions because I feel it is best to learn and play to see what way suits you best. I enjoy coding more in AS3 than in MXML, but I know that I need to use both.

    First you don't need to load Flash Professional as Flash Builder is an extremely powerful RIA developing tool. I have used it for years to build various application. If I need an application that deal a lot with timelines, then I switch to Flash Professional, but usually everything I need to do can be done in Flash Builder/Flex.

    Secondly, in regards to the language to use. This is what I usually do, most of my coding is done in AS3, this allows me to share specific classes with various different project without needing to manually port them over. If my stage will contain a lot of components that are static, then I usually build a MXML page for that and tie my coding in with AS3. Before I continue, I use Robotlegs for my Framework (per say) which is a pure AS3 framework. Using it anything static that appears on the screen are built using MXML and all the functionality behind it is controlled by AS3.

    Some people say that using AS3 you have a better chance of your code running much quicker. It is hard to say, but I do find that everything I need to do in an MXML page can be done using AS3 alone.

    So back to your questions, you'll have to find which method suits you, I prefer using AS3 as much as possible and MXML for all static stage display objects (datagrids, buttons, labels, and such). ALL of my functionality is controlled via AS3 and rarely place directly on a stage component. You will have to play around and see what method works best for you. Don't let any programmer drive you into one method over another because of their personal preferences.

    AS2 was basically a helper type language which augmented the stage components, however with AS3, it has become far more adaptable and functionality. If you look at the AS3 references you find a number of way to replicate the functions in Flex/Flash. The good part is if is isn't in the reference it doesn't mean it cannot be done. The ability to extend components makes it very versatile. There are thousands of links to people who have figured out simple (sometimes now so simple) way to complete a task in AS3. Just come up with an idea and step through what you want and how to make AS3 work for you.

    Not sure what ever I can say about that, play with it until you find a solution that works for you.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    20
    As always, very interesting and thought-provoking stuff here. Thank you all so much for taking the time to reply. It is much appreciated.

    Cheers!

  7. #7
    Member
    Join Date
    May 2012
    Posts
    51
    I don't think it makes sense to suggest to newcomers that it's all just a matter of personal preference. There're definitely situations where using Flex is out of the question. The biggest problem is file size. The Flex run-time library is close to a meg in size. Asking visitors to your web-site to stare at a preloading progress bar just because somehow you feel like using Flex is not nice. In a mobile scenario, that's flatly unacceptable.

    Perhaps there's a pure-AS3 UI framework out there that's smaller and tighter. Still you're potentially including a lot of stuff that isn't needed. And all of it has to be loaded on frame #1. So visitors to your web-site are once again staring at a either a blank screen or a progress bar.

    The biggest challenge for programmers coming to Flash, I think, is to embrace techniques that seems stupid. For example, if you need a number of push buttons that look the same but for the label, the programmer instinct would tell you to create a push-button class and pass different text strings to its constructor. As it turns out though, the seemingly retarded method of duplicating the button in Flash Professional and manually changing the labels yield a superior result, since FP can then efficiently subset the font employed, including only characters that are used into the SWF. Programmers often feel reluctant to put objects on the Flash timeline. It somehow seems "dirty." But the timeline gives you an intuitive way of adjusting the loading order of codes and resources, so that things appear immediately instead of requiring a 2-second wait.

  8. #8
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Cleong, that is your opinion. I have a different one. You seem to believe that your opinion is the only right one. I've trained many programmer to use Flex and Flash and have an idea how it works sometimes. You don't have to agree with my method and you are free to suggest other, but I find it pointless to argue a point about what you think I should do, especially if I am not asking for it.

    The user will do what they feel is right and if they have further questions, feel free to answer your way and I shall answer mine.
    Last edited by samac1068; 06-15-2012 at 12:03 PM.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  9. #9
    Member
    Join Date
    May 2012
    Posts
    51
    Well, while it would be sort of flattering to think that Adobe designed their software such that it'd work exactly in accordance to my completely obscure opinion, somehow I don't think that's the case. The features in question probably exist because Adobe believed they're what their customers need and want. But what do I know. Maybe I'm Jesus Christ after all and developers at Adobe are just trying to avoid eternal damnation.

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    9

    Fair point.

    You make a fair point.

    From my understanding when you use MXML you still have to use actionscript. Actionscript is the programming language and mxml documents are used to layout and hook trigger to your code.

    If I were developing a pure application, I would jump in and use Flex from the get-go.

    ------------------------------
    pirates game

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center