Starting with haXe


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /homepages/8/d317303823/htdocs/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /homepages/8/d317303823/htdocs/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /homepages/8/d317303823/htdocs/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

I heard a lot about haXe recently, so I made ​​the leap. This is a short introduction with demo to the haXe language.

haXe is a multiplatform language. You can use it to target Flash, JavaScript, C++, Php, and soon C# and Java. What does it mean ? You write your program in haXe and you chose on which platform you will export it. It is compiled on its target platform : .swf .php .js …

“The idea behind haXe is to let the developer choose the best platform for a given job. In general, this is not easy to do, because every new platform comes with its own programming language. What haXe provides you with is:
– a standardized language with many good features
– a standard library (including Date, Xml, Math…) that works the same on all platforms
– platform-specific libraries : the full APIs for a given platform are accessible from haXe”

In this short tutorial, I will target on two platforms .swf and binaries file (cpp) with the same code!

The result is quite simple : HaxeCircle, click on each circle to give it a random position.
Obviously, I don’t have any problem to do it in ActionScript3, but if you ask me to do it in CPP it will not be the same! I made some CPP two years ago, but I don’t remember well. So with haXe I will not have problem about this, and if tomorrow you ask me to do it in Java, I will be able!

Let’s start coding now :
First of all you need to install haXe : download link.
If you want to have a quick look on the language before programming : Migrating AS3 to haXe, haXe language reference.
I know two IDEs to code in haXe : FDT with a plugin and FlashDevelop. The second seems to be better.

Now create your first haxe project targeting .swf.
In your Main class (.hx extension file) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package;
 
import flash.display.Sprite;
import flash.events.MouseEvent;
 
class Main extends Sprite {
 
   static public function main() {
 
      new Main();
   }
 
   public function new() {
 
      super();
 
	  var circle:MyCircle;
 
	  for (i in 0...10) {
		  circle = new MyCircle(Std.int(Math.random() * 0xffffff));
		  circle.addEventListener(MouseEvent.CLICK, _click);
	  }
   }
 
    private function _click(mEvt:MouseEvent):Void {
 
		mEvt.target.x = Math.random() * flash.Lib.current.stage.stageWidth;
		mEvt.target.y = Math.random() * flash.Lib.current.stage.stageHeight;
   }
 
}

After a quick look, there isn’t lot of difference with AS3. Indeed, haXe is based on ECMAScript too. However you can notice a Std.int method to cast our float. It comes from C library!
The static public function main() is the entry point of the program and we call the constructor. All the constructor are defined in a method new() which cannot have a return.

The second class is the Circle :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package;
 
import flash.display.Sprite;
 
class MyCircle extends Sprite{
 
	public function new(color:Int) {
 
		  super();
 
	      flash.Lib.current.addChild(this);
 
	      var circle:Sprite;
 
		circle = new Sprite();
		circle.graphics.beginFill(color , 0.5);
		circle.graphics.drawCircle( 0 , 0 , 40 );
		circle.x = flash.Lib.current.stage.stageWidth * Math.random();
		circle.y = flash.Lib.current.stage.stageHeight* Math.random();
 
		addChild(circle);
	}
}

Now to compile your programm, create a .hxlm file and add :

-swf9 HaxeCircle.swf
-main Main
-swf-header 640:460:60:ccccff

Then open your terminal and run : haxe project.hxml
Your .swf file will be created. You have created your first programm in haXe 🙂

Now let’s start with the CPP part. It is not really hard, you use the same API than Flash!
In the terminal run : haxelib install hxcpp to install the hxcpp library on your computer.

Then open your Main class again, this is the new code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package;
 
import flash.display.Sprite;
import flash.events.MouseEvent;
 
import nme.Lib;
 
class Main extends Sprite {
 
   static public function main() {
 
      #if flash
            new Main();
      #else
            Lib.create(function(){new Main();},640,480,60,0xccccff,(1*Lib.HARDWARE) | Lib.RESIZABLE);
      #end
   }
 
   public function new() {
 
      super();
 
	  var circle:MyCircle;
 
	  for (i in 0...10) {
		  circle = new MyCircle(Std.int(Math.random() * 0xffffff));
		  circle.addEventListener(MouseEvent.CLICK, _click);
	  }
   }
 
    private function _click(mEvt:MouseEvent):Void {
 
		mEvt.target.x = Math.random() * flash.Lib.current.stage.stageWidth;
		mEvt.target.y = Math.random() * flash.Lib.current.stage.stageHeight;
   }
 
}

So what has changed ?
We added a lib in the import : nme.Lib; using by CPP and there is an other way to setup the “stage” in the main static function. You will notice that the call to the constructor change if you are targeting to .swf or binaries. Indeed there is only one line to change (comment) if you change your target, it is your nme.Lib import!
Now open the project.hxml and change it :
-main Main
-lib nme
-lib hxcpp
–remap flash:nme
–remap neko:cpp
-cpp cpp

Compile, it might be long the first time, and execute the binary file created. The result should be the same than flash!

To conclude, haXe is really a powerful language. Its lacks of documentation is a bit limiting to use it, but I’m sure it will be one of the most important language in a few years. So do not forget it 😉

1 thought on “Starting with haXe

Leave a Reply

Your email address will not be published. Required fields are marked *