{"id":339,"date":"2011-08-03T19:27:00","date_gmt":"2011-08-03T18:27:00","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=339"},"modified":"2014-11-01T15:05:46","modified_gmt":"2014-11-01T14:05:46","slug":"starting-with-haxe","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/starting-with-haxe\/","title":{"rendered":"Starting with haXe"},"content":{"rendered":"<p>I heard a lot about haXe recently, so I made \u200b\u200bthe leap. This is a short introduction with demo to the haXe language.<\/p>\n<p><a href=\"http:\/\/haxe.org\/\" target=\"_blank\">haXe<\/a> 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 &#8230;<\/p>\n<p>&#8220;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:<br \/>\n&#8211; a standardized language with many good <a href=\"http:\/\/haxe.org\/doc\/features\" target=\"_blank\">features<\/a><br \/>\n&#8211; a standard library (including Date, Xml, Math&#8230;) that works the same on all platforms<br \/>\n&#8211; platform-specific libraries : the full APIs for a given platform are accessible from haXe&#8221;<\/p>\n<p>In this short tutorial, I will target on two platforms .swf and binaries file (cpp) with <strong>the same code<\/strong>!<\/p>\n<p><!--more--><\/p>\n<p>The result is quite simple : <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2011\/08\/HaxeCircle.swf\" rel=\"lightbox[flash 640 480]\"><strong>HaxeCircle<\/strong><\/a>, click on each circle to give it a random position.<br \/>\nObviously, I don&#8217;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&#8217;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!<\/p>\n<p>Let&#8217;s start coding now :<br \/>\nFirst of all you need to install haXe : <a href=\"http:\/\/haxe.org\/download\" target=\"_blank\">download link<\/a>.<br \/>\nIf you want to have a quick look on the language before programming : <a href=\"http:\/\/haxe.org\/doc\/start\/flash\/as3migration\" target=\"_blank\">Migrating AS3 to haXe<\/a>, <a href=\"http:\/\/haxe.org\/ref\" target=\"_blank\">haXe language reference<\/a>.<br \/>\nI know two IDEs to code in haXe : <a href=\"http:\/\/www.fdt.powerflasher.com\/\" target=\"_blank\">FDT with a plugin<\/a> and <a href=\"http:\/\/www.flashdevelop.org\" target=\"_blank\">FlashDevelop<\/a>. The second seems to be better.<\/p>\n<p>Now create your first haxe project targeting .swf.<br \/>\nIn your Main class (.hx extension file) :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package;\r\n\r\nimport flash.display.Sprite;\r\nimport flash.events.MouseEvent;\r\n\r\nclass Main extends Sprite {\r\n\r\n   static public function main() {\r\n\t\r\n      new Main();\r\n   }\r\n   \r\n   public function new() {\r\n\t\r\n      super();\r\n\t  \r\n\t  var circle:MyCircle;\r\n\t  \r\n\t  for (i in 0...10) {\r\n\t\t  circle = new MyCircle(Std.int(Math.random() * 0xffffff));\r\n\t\t  circle.addEventListener(MouseEvent.CLICK, _click);\r\n\t  }\r\n   }\r\n   \r\n    private function _click(mEvt:MouseEvent):Void {\r\n\t\r\n\t\tmEvt.target.x = Math.random() * flash.Lib.current.stage.stageWidth;\r\n\t\tmEvt.target.y = Math.random() * flash.Lib.current.stage.stageHeight;\r\n   }\r\n\r\n}<\/pre>\n<p>After a quick look, there isn&#8217;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!<br \/>\nThe <em>static public function main()<\/em> 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.<\/p>\n<p>The second class is the Circle :<\/p>\n<pre lang=\"actionscript3\" line=\"1\">package;\r\n\r\nimport flash.display.Sprite;\r\n\r\nclass MyCircle extends Sprite{\r\n\t\r\n\tpublic function new(color:Int) {\r\n\t\t\r\n\t\t  super();\r\n\t      \r\n\t      flash.Lib.current.addChild(this);\r\n\t\r\n\t      var circle:Sprite;\r\n\t\r\n\t\tcircle = new Sprite();\r\n\t\tcircle.graphics.beginFill(color , 0.5);\r\n\t\tcircle.graphics.drawCircle( 0 , 0 , 40 );\r\n\t\tcircle.x = flash.Lib.current.stage.stageWidth * Math.random();\r\n\t\tcircle.y = flash.Lib.current.stage.stageHeight* Math.random();\r\n\t\t\r\n\t\taddChild(circle);\r\n\t}\r\n}<\/pre>\n<p>Now to compile your programm, create a .hxlm file and add :<\/p>\n<pre>-swf9 HaxeCircle.swf\r\n-main Main\r\n-swf-header 640:460:60:ccccff<\/pre>\n<p>Then open your terminal and run : <em>haxe project.hxml<\/em><br \/>\nYour .swf file will be created. You have created your first programm in haXe \ud83d\ude42<\/p>\n<p>Now let&#8217;s start with the CPP part. It is not really hard, you use the same API than Flash!<br \/>\nIn the terminal run : <em>haxelib install hxcpp<\/em> to install the hxcpp library on your computer.<\/p>\n<p>Then open your Main class again, this is the new code : <\/p>\n<pre lang=\"actionscript3\" line=\"1\">package;\r\n\r\nimport flash.display.Sprite;\r\nimport flash.events.MouseEvent;\r\n\r\nimport nme.Lib;\r\n\r\nclass Main extends Sprite {\r\n\r\n   static public function main() {\r\n\t\r\n      #if flash\r\n            new Main();\r\n      #else\r\n            Lib.create(function(){new Main();},640,480,60,0xccccff,(1*Lib.HARDWARE) | Lib.RESIZABLE);\r\n      #end\r\n   }\r\n   \r\n   public function new() {\r\n\t\r\n      super();\r\n\t  \r\n\t  var circle:MyCircle;\r\n\t  \r\n\t  for (i in 0...10) {\r\n\t\t  circle = new MyCircle(Std.int(Math.random() * 0xffffff));\r\n\t\t  circle.addEventListener(MouseEvent.CLICK, _click);\r\n\t  }\r\n   }\r\n   \r\n    private function _click(mEvt:MouseEvent):Void {\r\n\t\r\n\t\tmEvt.target.x = Math.random() * flash.Lib.current.stage.stageWidth;\r\n\t\tmEvt.target.y = Math.random() * flash.Lib.current.stage.stageHeight;\r\n   }\r\n\r\n}<\/pre>\n<p>So what has changed ?<br \/>\nWe added a lib in the import : nme.Lib; using by CPP and there is an other way to setup the &#8220;stage&#8221; 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!<br \/>\nNow open the project.hxml and change it :<br \/>\n<em>-main Main<br \/>\n-lib nme<br \/>\n-lib hxcpp<br \/>\n&#8211;remap flash:nme<br \/>\n&#8211;remap neko:cpp<br \/>\n-cpp cpp<\/em><\/p>\n<p>Compile, it might be long the first time, and execute the binary file created. The result should be the same than flash!<\/p>\n<p>To conclude, haXe is really a powerful language. Its lacks of documentation is a bit limiting to use it, but I&#8217;m sure it will be one of the most important language in a few years. So do not forget it \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I heard a lot about haXe recently, so I made \u200b\u200bthe 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 &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/starting-with-haxe\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Starting with haXe<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[70,6],"tags":[15,73,72,71,74],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/339"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=339"}],"version-history":[{"count":7,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":1295,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/339\/revisions\/1295"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}