Log in

No account? Create an account

Hello-world in Flash/Flex

« previous entry | next entry »
Aug. 3rd, 2006 | 03:19 am

It's a popular misconception that writing applications for the Flash platform is extremely difficult and requires one to use an IDE and be intimately familiar with timeline-based development.

In reality, you can write the most feature-rich Flash applications with just a text editor and a command-line compiler.

Here's what a simple hello-world application looks like in plain Flash (version 9):

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

  public class HelloWorld extends Sprite {

    public function HelloWorld() {

      var textField:TextField = new TextField();
      textField.text = "Hello, world!";

      trace("Hello, world!");

Write this in a file called HelloWorld.as. Compile it with mxmlc (Flex command-line compiler).

C:\>mxmlc HelloWorld.as
Loading configuration file C:\Program Files\Adobe\Flex Builder 2
\Flex SDK 2\frameworks\flex-config.xml
C:\\HelloWorld.swf (634 bytes)

mxmlc generates a file called HelloWorld.swf (634 bytes). Open the file in your web browser (requires the Flash Player 9 plug-in) to see the output.

The trace statement is for debugging: the output will show up in the player logs.

Now, this looks a lot like maybe Swing or Qt. It's the '90s way of writing applications. Let's write a hello-world in Flex then:

<?xml version="1.0"?>
<Application xmlns="http://www.adobe.com/2006/mxml">
  <Label text="Hello, world!" />

There you go! That looks a lot like HTML or XUL, doesn't it?

Save this in a file called HelloWorld.mxml, compile it with mxmlc, and see the output - again, a file called HelloWorld.swf (1,16,945 bytes).

Why's the Flex output more than 100x heavier? Because it's a UI framework. You get a lot of stuff for free; for example, the text "Hello, world!" in the Flex version is automatically center-aligned and has a nice-looking sans-serif font by default (which can be changed using CSS syntax).

Next time someone tells you that writing Flash applications is difficult and not their cup of tea, please send them here.