Getting Started (Flash)

Developing Flash applications is really easy with Haxe. Let's see our first HelloWorld sample :

This is a very simple example showing most of the toolchain.

Making a folder
Create a folder in whatever directory you like and call the folder "HelloWorld". Then, open the folder.

Test.hx
Define/create this class in a file named Test.hx

Creating the Build Script
Create the file compile.hxml in the same directory with the following content.

Compiling your flash application
To compile, you can simply double-click on the compile.hxml file or run the command haxe compile.hxml. If any errors occur, they will be displayed.

If you get the error "Standard library not found" or "Class not found : Test", make sure Haxe can find both the library files distributed with Haxe (the contents of the std directory) and the Test.hx file itself. By default, Haxe looks in the current directory only, meaning that both Test.hx and the contents of the std directory need to be in the directory from which Haxe is invoked. You can override the search path by setting the HAXE_LIBRARY_PATH environment variable to e.g. /home/mjs/local/lib/haxe/std:. (note the final colon and period).

If you get the error "Invalid class name -swf9 test.swf" change the encoding of the .hxml file to ANSI.

test.html
If everything goes smoothly like it should, this will produce a file named test.swf. Now create an HTML page such as this one and name it test.html: height="300" id="haxe" align="middle">       

If you open the file test.html with your web browser, it should display Hello World with information about the file and the line the trace occured.

Drawing a Square
So far we've been only using pure Haxe, not Flash-specific API. The trace function is implemented by default so that it will trace on the screen. Let's now see how to draw a square on the screen. For that we need to get a drawing context, which is called a MovieClip in Flash. Modify your Test.hx file with the following content :

}

What this code is doing is getting the current MovieClip and using the Flash drawing API to display a square.

Run the compile.hxml file to compile again and open the test.html to display a red square.

Using the Library
In Flash, you store assets (graphics, sounds, fonts...) inside the Library. Resources can have an identifier called a //linkage name//. When you create a SWF file from a Library using the Flash IDE or other third party open source tools such as the recommended [|SwfMill], it will contain the resources that can be referenced from Haxe using their //linkage names//.

For example, let's say you have a SWF named resource.swf containing a MovieClip with //linkage name// button. Modify your code so that it will display this resource :

This sample will attach a button from the library at //depth// 0 and with //identifer// but001 (there can be only one unique object per identifier and depth). It will then place the button at position (10, 20) on the screen.

In order to display the button, we need to tell Haxe to use our resource.swf file to look for resources. Edit the compile.hxml file with the following content :

You can now run the compile.hxml file to compile the project. The test.swf created this way will include the entire content of the resource.swf as well as the Haxe compiled code. You can test it by opening the test.html file.

Please note that the test.swf will also use the same parameters (width, height, background color, flash version and frames-per-second) as specified the resource.swf while without the resource, it will use Haxe default parameters. However most of these parameters (except the frames-per-second and flash version) can be overridden in the HTML source as you can see inside the test.html file.

Changing SWF properties
The SWF properties can be changed by using the -swf-header commandline flag. Edit your HXML and add the following :

That will set the SWF size to 200x300 pixels at 40 FPS with a red background color. Please note that the size and background are redefined in the HTML properties.

You can also choose to target a specific Flash Player version, by using the -swf-version commandline flag. Haxe will process accordingly : * version 6 : will generate bytecode for Flash Player 6.0 (slower since it doesn't use registers). The flash API are not disabled with this version so be careful to only use the API classes/methods available for this player version. * version 7 : will generate bytecode for Flash Player 7, while disabling Flash 8 API * version 8 : will generate bytecode for FP8 (same as FP7) but enable Flash 8 new API * version 9 : will target Flash Player 9 and use the ActionScript3 API

Changing the security sandbox
If would like your local swf to access network resources (but not filesystem ones), you can set the security sandbox of the swf file to local-with-network by adding

to your build.hxml or command line.

Going Further
Now that you can do such easy things, all you need to go further is to learn the Flash API needed to implement your application. Haxe is conservative about the API so you can read the [|Adobe Livedocs for Flash 6-8] (see the "ActionScript Language Reference") or the [|Adobe Livedocs for Flash 9/10] in order to get full help about it.

There are some minor differences between Haxe Flash API and ActionScript 2 Flash API, which are listed here :

* the standard classes such as Date, Array, String can have some changes in Haxe since they are common to all Haxe platforms. Watch the Haxe API documentation for more details.

* the XML and XMLNode classes are merged into one single Xml class, and provides and extended API.

* all the core Flash classes MovieClip, TextField... are in the flash package. You need then to use flash.MovieClip instead of MovieClip, or add an import flash.MovieClip; statement at the beginning of your class.

* use flash.Lib._root instead of _root, flash.Lib._global instead of _global and flash.Lib.current to access the //local root// (different from _root if the Haxe code SWF was loaded).

Apart from these minor differences, you can easily use all of the existing Flash API if you know it already.