O'Reilly logo

Learning Flex 4 by Elijah Robison, Alaric Cole

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ActionScript's Relationship with MXML

As we’ve seen, MXML is great for laying out structure, and ActionScript is suited for defining method logic and interaction. So what is the difference between the two? In many ways they’re the same thing.

MXML and ActionScript Work Together

MXML and ActionScript complement each other. You’ll find it’s easier to build an application’s structure using MXML than using just ActionScript. MXML unfolds faster, it’s easier to read and write, and you get results from fewer keystrokes. But MXML doesn’t replace ActionScript. Scripting is quite necessary for serious Flex applications. In fact, if anything it’s MXML that’s unnecessary, because if you wanted, you could build a full application using just ActionScript.

MXML Becomes ActionScript

When you compile an application, all the MXML you created is initially interpreted into ActionScript, and then it’s processed into a binary SWF. You can think of it like this: ActionScript is the language of Flash Player, and everything in Flex is eventually distilled into ActionScript.

Tags Are Classes

MXML components in a Flex application can be reduced to equivalent ActionScript code. For example, the following code creates a Button in MXML:

<s:Button id="myButton"/>

But you can also add a Button using ActionScript:

import spark.components.Button;
var myButton:Button = new Button();
addChild(myButton);

Knowing this, you can use ActionScript to create components dynamically, without having to rely on MXML.

Attributes Are Properties ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required