TutorialTutorial: Getting started with haXe

haXe (pronounced hex – yes I know it’s confusing) is a relatively new language.  It is very similar to ActionScript3, so if you’re from an AS3 background, you won’t have to learn a lot of new things.

In this tutorial, we will be installing haXe and FlashDevelop, configuring FlashDevelop for haXe, and creating a simple “Hello World” program.

Difficulty: Beginner
Knowledge: Some ActionScript would help

haXe has several benefits over ActionScript.  First off, the haXe compiler is smart.  It compiles .swf files that are faster than files compiled from AS with Adobe’s compiler.  Second, Adobe has created this thing for Flash called “Alchemy.”  Alchemy is a really fast way to access memory.  ActionScript3 currently cannot use Alchemy, but haXe can.  Using this type of memory, accessing variables is magnitudes faster than an array.  Also, haXe has language benefits over ActionScript, like strict typing, templates, and inlining.  Lastly, haXe can compile to more than just Flash .swf.  It can compile C++, PHP, JavaScript, and Neko. More info on haXe is on the website.

So first off, you need to download haXe.  You can get it from the website, the download page is here:  Download haXe
If you are using Vista or higher, you will need to run the program as an administrator.  Just double clicking on the installer won’t work.
Installing haXe may take some time.  It downloads the files needed then unpacks them.  You may be prompted to install Neko.  If you want to compile your code to C++, you will need to install this.

Now that you have haXe installed, you probably need some sort of IDE to write your code in. I highly recommend FlashDevelop. The latest version (3) supports haXe natively, so there are no plugins to install. It sports code intelligence, auto-completion, syntax coloring, built-in debugger, and many other features. Plus, it’s completely free. We will be using it for this tutorial. It is available for download here.

The latest version will be the top item.
Scroll to the bottom of the post, and you’ll find the link.
Installing FlashDevelop is pretty simple.  You can just keep hitting next.  After it’s installed, open it up.
Before we can start coding, there are some things that need to be configured first.  So let’s open the Program Settings.  You can do that by going to Tools->Program Settings or by hitting F10.
This is what the program settings window looks like.  It’s a little overwhelming at first.  There’s a lot of options.  What we are interested in right now is “HaXe Context.”  Properties will change to bold if they are not the default value.  The most important option is “HaXe Path.”  Set that to where haXe is installed, which probably is C:\Program Files\Motion-Twin\haxe (Program Files (x86) if you’re on a 64-bit machine).  I have set “Default Flash Version” to 10, so that I can use Alchemy and other new Flash features.
FlashDevelop needs to be told how to open .swf files.  Select “FlashViewer”.  Set “External Player Path” to the location of the Flash Player you have installed on your system.  If you already have Adobe Flash (the development program) installed, it would be somewhere like:  C:\Program Files\Adobe\Adobe Flash CS4\Players.  I highly recommend you use a debugger player, so that you can debug your code using FlashDevelop.
And the last thing to configure is where the haXe libraries are.  Go to Tools->Global Classpaths (or Ctrl+F9).  Select HaXe from the drop down box, then add the location of where you installed haXe plus \std tacked onto the end.  So the path will probably be C:\Program Files\Motion-Twin\haxe\std
Now that everything is installed and configured, we can actually start coding.  Hooray!  We’ll do a classic hello world example.  To start, create a new project in FlashDevelop.  Go to Project->New Project.  Scroll down and select “AS3 Project” under the HaXe category.  Name the project “Hello World” and choose a place to save it.  Hit OK.
On the right, you will see a “Project” tab.  Open that and drill down to src->Main.hx and double click.  This is a skeleton file that you put your main code in.
In the main function, type:

trace("Hello World!");

Save the file, and click the blue play button near the top (or press Ctrl+Enter like in Flash).  If all goes well, Flash Player should pop up, and there will be a single line of text that says “Hello World!”
Notice that haXe trace statements output on top of the .swf itself, not to a debug window like in Flash.  If you want to hide trace statements, just change “Debug” to “Release” by the play button.

You can leave a response, or trackback from your own site.

5 Responses to “Tutorial: Getting started with haXe”

  1. [...] Tutorial: Getting started with haXe – +1 Coins [...]

  2. James Graham says:

    Exactly what I was looking for! Thanks!

  3. simpator says:

    not working install all haxe stuff with original haxe installer
    got this error when compiling hello word example on flashdevelop

    Running process: C:\Program Files\FlashDevelop\Tools\fdbuild\fdbuild.exe “F:\HAXE_all\examples_all\Start.hxproj” -ipc 79ad94f0-8d0d-4d34-93a4-758b29c294c2 -compiler “C:\Program Files\Motion-Twin\haxe” -notrace -library “C:\Program Files\FlashDevelop\Library” -cp “C:\Program Files\Motion-Twin\haxe\std”
    Building Start
    haxe -cp “C:/Program Files/Motion-Twin/haxe/std” -cp src -neko F:/HAXE_all/examples_all/bin/Start.n -main Main
    ‘nekoc’ is not recognized as an internal or external command,
    operable program or batch file.
    Error : Neko compilation failure
    Build halted with errors (haxe.exe).
    Done(1)

    • dotMaiku says:

      It looks like you’re trying to compile a neko project. If you don’t have neko installed (which it looks like you don’t) it won’t work =P

      When you create a new project, make sure you select “ActionScript” as the type.

      Go to Project->New Project. Scroll down and select “AS3 Project” under the HaXe category.

  4. Thanks for putting this tut up – just what I needed. Now I have a problem which I have seen posted elsewhere but have not found a solution that works for me:

    Using FD 4 RC2

    Compile with Global classpath to std yields this error message: “Please don’t add haxe/std to your classpath, instead set HAXE_LIBRARY_PATH env var”

    I set this env var.

    I removed it from global classes and now get this error instead: “Standard library not found”

    In no case is a swf generated.

    Any suggestion how to fix this? like I said, I have tried many fixes posted around and even reinstalled everything once.

    Thanks in advance,

    Dave

Leave a Reply