haXe : raytrace v2.0.0

This started of as my own little utility but then I use it in almost all the flash, flex, haXe applications when output is SWF. Now its available as a opensource project in github and the final compile dfiles are here, if you just want to use it.
This is nothing more than a logger named raytrace. It takes string as the only parameter and the implementation is done in haXe and NME.
Just put the “raytrace.as” in the root folder of your flash/flex application and then from anywhere in your code one can write

raytrace('my message');

Then this message will be visible in this RayTrace swf. Both the “.as” file and the “.swf” are packaged here as a zip file for your no time getting started.

Happy haXe -ing

RayTrace : An external logger for flash

Well, with my moving forward with haXe and NME, I needed an external logger for flash targets. Though there is a trace message view by default in haXe, it is necessary to have an external logger for me. RayTrace is written in haXe and can be used in any flash and flex based applications not only written in haXe. So if you are doing a pur actionscript or flex project, still you can use RayTrace as your logger. It comes with a client class for flash based projects, which is written in actionscript. Just put the file in the root directory and you are good to go. From anywhere in your actionscript code do a raytrace as below.

raytrace('my message');

The message you send from your code will be visible in the RayTrace logger. Currently it simply supports the string message only.

The source code is available in github as well so as if you want can go ahead and write your own implementation.

happy haXe -ing

haXe : dynamic datatype

It may be surprising to believe that the data types are static in haXe, though typed dynamically! This is awesome, for it gives you freedom to just type a variable without defining its datatype at the same time it will not allow to assign other data types to be assigned to the same variable. The best of both worlds. Whether you come from a dynamic language like javascript or a statically typed language like actionscript 3, this will surprise you in the beginning for sure. Another cool thing (rather hot thing) about haXe is these datatype mismatch will be checked at compile time and if mismatched, will not be compiled! Just beautiful.
Here is an example code.

private function testDataType()
	{
		var a;
		a = 2;
		trace(a);
		a = '2';
		trace(a);
	}

The compiler will throw an error saying “String should be Int”. But how it knew that the datatype is Int ?!! The catch is, when the variable is assigned a value for the first time, the datatype is defined for that variable at that point and there after can not be changed. In our case we defined it as Int when assigned the value for the first time.

This concept goes further to function arguements and return types as well.

private function testDataType(x)
	{
		trace(x);
	}

If we call above function with two different types of data as

testDataType(2);
testDataType('test');

The compiler will trow an error saying data type mismatch.
Lastly if there is a return type example code is as below.

	private function testDataType(x)
	{
		if (x==2)
		{
			return 2;
		}else {
			return 'hello';
		}
	}

Though we can assume to write something like above in a dynamic language, but haXe compiler will not compile this, instead throw an error saying data type mismatch.
Happy haXe -ing. :)

haXe : doing a regular update and NME

This came to me as a surprise while trying to write a script, which can run in iOS, HTML5, Flash and Neko. I knew it can be done by cross-compiling through NME to all these target platforms. While this hold good almost in every case but when it comes to regular stage update or the ENTER_FRAME event, it is a problem.
Well, if you are a flash developer and depend on ENTER_FRAME event for a long time, then it may come to you as a surprise like me. The game loop as the gaming engines call it, now has to be worked upon. Fortunately we have a timer function in haXe and that quite fits the game.
Before going ahead in timer, lets see the unreliable nature of ENTER_FRAME for all targets. Firstly I was creating different circles in stage area with an ENTER_FRAME, while that worked well in all platforms, I thought its good to go for all. But then I tried to rely upon the same ENTER_FRAME event with a drag and drop and position change of the elements. Whoa!! That does not work! I stopped relying on the ENTER_FRAME event and changed that with Timer and everything is back in the action again.
The haXe timer code is a little different than Flash timer and the code looks as below.

var timer = new Timer(10); //10 is the time difference in milliseconds between the timer call
timer.run = onEachTimerTick;//its kind of event listener
function onEachTimerTick():Void {}

For me, ENTER_FRAME event only worked for Flash targets and nothing else. So be careful if you want to target other delivery methods.

Happy coding!

haXe : A for loop

Well, this may be a simple case to write. But there is a chance that you may get it wrong in the first go. Thats for this kind of loop has got a slightly different syntax.
The syntax for a for-loop in haxe looks as below

for(i in min...max)
	{
		trace('This is inside for loop.');
	}

This loops from a value of “min” to “max” and “i” is the iterator.

Thats quite simple, even simpler than for-loops in other languages!
Happy haXe-ing :)

NME 3.1 RC4 is released

Well moving ahead with haXe and NME is as exciting as the new release of NME library. Though this is a minor release to RC4, but looking at the speed of its release cycle, it seems we are just fine learning the new options in NME, where the NME development team is doing their best to give us new features soon.
If you are like me and coding in MAC then there is definitely a point when you think how to install the new libraries into my development environment. Actually its as simple as putting the downloaded library in right folder and setting the current environment to point to the latest version.
It may be required depending upon your machine settings that you may require to configure your machine so as to show all the hidden and system files, which are generally hidden.
All the supporting libraries of haXe, remains inside haXe folder. So just navigate to

/usr/lib/haxe/lib

And there you will find all the library folders.Now go inside “nme” folder and the current version of the library will be present there. All we have to do is, download a new version of the library and put it in this folder. I have recently downloaded the 3.1RC4 and extracted it into this folder. The main folder name I kept as “3,1,RC4″, keep a note these are comma (3,1,RC4) thats how haXe reads them. So its just a practice. Now there is a file inthis folder with name “.current”(This file is generally hidden). Here the currently active version of the lib is specified. Open this file in a text editor and remove everything else (If there is anything at all there). Then type “3.1.RC4″ as the only text in the file. Just remember when we name our folder as comma separated values, haXe reads them as dot(.) while reading from the “.current” file, so we have to just follow this instruction.
That all to it. If you fire up the Terminal in MAC and type

haxelib list

then the output will show something like

nme: 3.0.1 [3.1.RC4]

The version with a square bracket is the currently active version.
So get the latest lib and activate it in development environment and have fun :)

haXe entry point

Lets get down to coding. The first class of ours in haXe is the main class or the program entry point class. The name of the class may be anything but the point to remember here is the entry point is a static function named main. So the first class of a haXe application looks as below.

package;
class Main {	
	public function new(){
	}
	
	public static function main() {
		trace("Hello haXe !");
		new Main();
	}
}

If you keep a closer look, the main class is not a display object, unlike the typical Flash application. Rather we have a general class here which is saved as “Main.hx” and this class has a method called “main()”. This is typical haXe and thats all we need to compile our program to a SWF.
We can save our compiler parameters in a file named “compile.hxml” or something similar as long as we can refer to it as a build file and having a “.hxml” extension. This file is run by the haxe compiler. In our particular case this file looks as

-swf9 bin/swf/Main.swf
-main Main
-cp src

Here we are defining the target type to be produced from the haXe compiler. Then the program entry class and next is class path.
Running this will produce a SWF named “Main.swf” in the folder “bin/swf/Main.swf”.
Quite straight forward and simplicity at its best.

some cool haXe IDE

Well developing in a language without a specific IDE for itself might be too much for a beginner. Fortunately we have some very cool and nice IDEs available for us.
1. FlashDevelop (Based on SciTE)
2. FDT5 (Based on Eclipse)

While both these IDEs are free, FlashDevelop is windows only. But I am surely in love with FlashDevelop. FDT is good but being on Eclipse, it takes up a lot of memory, on the other hand FDT runs on all platforms. So choose your weapons and get ready to haXe it all.

haXe basics

The bare bone basic of haXe language is one must need an Entry point to the Application. This could be any class, but then one must specify the entry point function, which is “main()” , the signature of function looks like

public static function main() {}

Constructors in HAXE are written as “new()” as below

class GameStage
{
 public function new(){}
}

the package declaration ends with a semicolon (unlike any other major laguages, it does not contain curly braces)

package com.saumya.haxe.helloWorld;
class GameStage
{
	public function new() {}
}

If you are extending an display object, then “super” must be called from the constructor. It snot optional, its mandatory.
Every line of code must end with “;” semicolon, again this is mandatory not optional.
Every class file is saved with “.hx” extension.
The haxe compiler file is saved with “.hxml” extension.
The NME compiler file is saved as “.nmml” extension.
A typical “.hxml: file looks as

-swf bin/swf/inTheBigBrain_as3_FP10.swf
-main Main
-swf-header 800:600:30:000000
-cp src

Similarly to compile from NME, you have to create a “.nmml” file.
A typical “.nmml” file description is here.

for compiling a .hxml file, you fire up the following command,

haxe test.hxml

Have a look at the detailed options.

To compile a nmml file, you have to fire up one of these commands. But the very basic is

haxelib run nme test my.nmml flash

The detailed options are here.

Hope that gives you the very basics of the language.