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.
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.
First, install Apache FlexJS 0.7 or newer. The Apache Flex SDK Installer can walk you through the installation process:
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 folder for your project, and name it HelloWorld
.
Inside the new project, create a new folder named src
. This is where our ActionScript classes will go.
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" );
}
}
}
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.