Hello World: How to set up Apache FlexJS to transpile ActionScript to JavaScript

by Josh Tynjala

After years of working with a powerful language like ActionScript, JavaScript often feels like a downgrade. Wouldn't it be better if you could build HTML applications using proper classes, interfaces, and compile-time type checking? With Apache FlexJS™, you can transpile ActionScript to JavaScript. No plug-ins required.

HelloWorld.as => asjsc => HelloWorld.js

Apache FlexJS gives you full access to the DOM, and it's even possible to reference JavaScript libraries in ActionScript. Let's get started with a simple example.

Install Apache FlexJS

First, install Apache FlexJS 0.7 or newer. The Apache Flex SDK Installer can walk you through the installation process:

Screenshot of Apache Flex Installer

Alternatively, if you have Node Package Manager (NPM) installed, you may use the following command to install Apache FlexJS from the command line:

npm install -g flexjs

Create a new project

  1. Create a new folder for your project, and name it HelloWorld.

  2. Inside the new project, create a new folder named src. This is where our ActionScript classes will go.

  3. Inside the src folder, create a file named HelloWorld.as, and add the following code:

    package
    {
        public class HelloWorld
        {
            public function HelloWorld()
            {
                var button:HTMLButtonElement = document.createElement( "button" ) as HTMLButtonElement;
                button.innerHTML = "Press Me";
                document.body.appendChild( button );
    
                button.addEventListener( "click", button_clickListener, false );
            }
    
            private function button_clickListener( event:MouseEvent ):void
            {
                alert( "Hello World" );
            }
        }
    }
    

This class will create a

Looking for more tutorials like this one?

Would you like to learn more about transpiling ActionScript? Please become a patron, and support the creation of a whole library of detailed, step-by-step tutorials — including video tutorials! Let's work together to teach the next generation of ActionScript developers.

Become a Patron