Getting started with haXe/Flash

posted by frank at Jul 1st, 2010

If you never heard about haXe before, you definitely missed the interception of one of the most powerful programming languages to date. It compiles extremely fast, it is light-weight, it focuses on generating safe and correct code. It has very accurate error reporting. It is neither strict nor duck typed, but instead delivers the best of both. It gives you back the feeling of the Turbo, which was lost long ago. It makes you consider writing code for fun!

Is it only about Flash? No, of course not. And yes, there are alternative implementations of the Flash APIs. For instance look at neash or lightspark.

Ok, here it comes: My first baby steps in Flash coding. The first demo taught me how to use the flash.display.Graphics API and the basics of display lists (n-ary trees actually). Can you get a L-System tree generator in 23 lines of code?

The build options for the haxe compiler (compile.hxml) are as follows:

-swf9 ltree.swf
-main TreeSprite
-swf-header 300:400:10:FFFFFF
Click on the image to see how to embed the flash code into your web site.


class TreeSprite extends flash.display.Sprite {
    static function main() {
        var stage = flash.Lib.current.stage;
        flash.Lib.current.addChild(new TreeSprite(stage.stageWidth, stage.stageHeight));
    }
    public function new(w: Int, h: Int) {
        super();
        graphics.lineStyle(2, 0x008000);
        treeGen(w/2, h, 100, -Math.PI/2, 5, 3);
    }
    private function treeGen(ux: Float, uy: Float, l: Float, phi: Float, n1: Int, n2: Int) {
        var vx = ux + l * Math.cos(phi);
        var vy = uy + l * Math.sin(phi);
        graphics.moveTo(ux, uy);
        graphics.lineTo(vx, vy);
        if (n1 != 1)
            treeGen(vx, vy, 0.8 * l, phi - Math.PI / 45, n1 - 1, n2);
        if (n2 != 1) {
            treeGen(vx, vy, 0.4 * l, phi - Math.PI / 4, 5, n2 - 1);
            treeGen(vx, vy, 0.4 * l, phi + Math.PI / 4, 5, n2 - 1);
        }
    }
}

Tags: haXe, Flash, fun


Comments

No comments yet.

Add a comment





I accept the private privacy*.

simple_captcha.jpg
Are you human?

PreviousBack to blog