At the bottom of the window you can find Haxe project types, choose the “JS Project”, give it a name and optionally a package (which will help organizing the code), click “OK”, and you’re done.
The main generated file should look like this:
static function main()
Here’s what’s happening in the code:
line 1 The package also shows the directory the file can be found in. Remember to put a semicolon after the package.
lines 5-8 A multiline comment
lines 10-16 Here our base class is being declared, with the default name “Main”. Inside you can see a single static function called “main”. This is the function which will be automatically executed after the program loads. For now it doesn’t do anything.
You can already run this program by pressing “F5”. It should open a blank page in your default browser.
Now we’re going to add some code to make sure it does something. Let’s use a static function from the Lib class. Once you type
Lib. FlashDevelop will show you the list of functions and properties available with some info about each one. If you select alert and press “Enter” it will autocomplete the name of the selected function.
alert and display some text, the line should read
Lib.alert("Hello world from Haxe!"); (again, don’t forget the semicolon).
If you run the program again (pressing ”F5” or selecting ”Project->Test Project” menu) you should see a standard pop-up:
It works, but what’s exactly happening here?
First the compiler takes your Haxe code, and generates a single ”. js” file from it, you can find it in the ”bin” folder of the project.
That file is being used in the ”index.html”, which was auto-generated by FlashDevelop when you created the project. If you open that file you’ll see that it’s mostly boilerplate HTML with two Haxe related lines:
If you replace the Lib.alert line with
trace("Hello world from Haxe!"); and run the program now you’ll see that it outputs
Main.hx:15: Hello world from Haxe! which is both the text you wanted, but also the exact file and line from which it came, very useful when debugging.
To be continued….