haXe Journey: The Gentle Introduction Part 1
Your First Flash Program
So you've taken the very minimal approach to haXe. You have notepad.exe and you have installed haXe using the Windows installer. You should be good to go. Anything else would only confuse the learning process.
With the basic set of tools you'll want to create a folder for your first test project. And just because you're obsessive compulsive, you'll also want to create that folder in a 'haxe' folder (or something to that effect) because you will no doubt be creating other projects using haXe.
After you have created your folder \haxe\scratch—preferably using the command shell and mkdir, trust me... using the command shell will make things much faster—you will be creating the basic files for building and testing haXe flash applications.
Create a Main.hx file. Case sensitivity is important so not main.hx or MAIN.HX or anything like that. In that file write the code that will be executed.
Main.hx
class Main {
static function main() {
trace("Hello World!");
}
}
That is the basic layout of the program. Don't worry about the class or the static bits. Just know that you have defined a function—executable sub-routine—named main() that doesn't take any arguments and calls the function trace() Note: whenever I talk about functions I will include the parenthesis.
Now we will create the build file. In order to correctly create a myawesomeflashthingy.swf you have to tell haXe what it's going to build and how. Create a file called build.hxml and put this stuff in it.
build.hxml
-swf program.swf
-swf-version 9
-main Main
In that file you're telling haXe to output the compiled flash program as program.swf using Flash 9 and that the entry point (a.k.a main() function) is in the Main class meaning the Main.hx file. We aren't tied to any old code so we are going to commit ourselves to the Flash 9 library. Note: That means that you will reference the 'flash9' API rather than the older 'flash' API.
Now you just need to tie the program that will be spat out into an HTML page to view it. Just create the test.html.
test.html
<html>
<head><title>haXe Flash</title></head>
<body bgcolor="#dddddd">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="400"
height="300"
id="haxe"
align="middle">
<param name="movie" value="program.swf"/>
<param name="allowScriptAccess" value="always" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="salign" value="lt" />
<param name="bgcolor" value="#ffffff"/>
<embed src="program.swf"
bgcolor="#ffffff"
width="400"
height="300"
name="haxe"
quality="high"
align="middle"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
/>
</object>
</body>
</html>
That is the twice-cooked embed method of flash. It's used because it is reliable. But you'll notice that the embed parameters are duplicated for both the <object> tag and the <embed> tag. So if you change the name of the swf file be sure to change the HTML in two places. Note: If you add a '-swf-header' declaration to your build file make sure that you remove the 'bgcolor' from the 'object' and 'embed' parameters or else those parameters will always override the header parameters.
The rest is cake. You can either double click the build.hxml file to compile the Main.hx file or from the command line you can just:
> build.hxml
If everything worked correctly then program.swf will exist in your directory. Since the file had a .hxml extension Windows knew what you were trying to do with. Then just launch the test HTML.
> test.html
Your favorite browser will pop up with the test page. In the test page will be a white rectangle with the print of "Hello World!" in it. Since we printed with the trace() function you also get the module and line number that the call to trace() was made.
If you look at the structure of what's in your directory you should see something that looks like this.
+ scratch
|
+-- Main.hx
|
+-- build.hxml
|
+-- program.swf
|
+-- test.html
You're just going to leave that alone. When you start your next project just copy and paste the contents of that folder into a new folder—that would made under the haxe directory—and then modify what you need to. Because in reality we just wrote all of the boilerplate code for a basic Flash program.
We use this for our next project.
