haXe : Adding a display object

Well, in our last tutorial we have already written a fair entry point to any haXe application. Now lets see how we will add a custom display object to our project.
A new display object class is written as below and saved as “VisualBox.hx”.

import flash.display.Sprite;
class VisualBox extends Sprite
{
	public function new()
	{
		super();
		trace(this);
	}

}

Since this is a visual class, it will extend a display object and thats what we are doing already. Nice and fine, but one thing to remember is, we have write a method named “new()” in order for that display object constructor. Then the “super()” call is necessary as for any display object.
Now lets see how are Main.hx looks now.

package;
import flash.Lib;

class Main {
	public static function main() {
		trace("Hello From FDT haXe !");
		new Main();
	}
	public function new(){
		var vb:VisualBox=new VisualBox();
		Lib.current.addChild(vb);
	}
}

Here is something happening! First if we have to add something to the current stage, we have to get it as “Lib.current”. Since we are using Flash for output, the path looks as “flash.Lib.current”. We have already imported flash.Lib, so we just have to access it as “Lib.current”.
Another thing to note is that, since Main.hx and the “VisualBox.hx” are on the same package, the package definition for “VisualBox” is omited from the file. Just look at the code above and see that “VisualBox” is not nested inside another package definition!
Happy haXe ing!