Haxe examples

This page lists some examples of simple haxe programs. Haxe is an open source compiler which generates flash programs. Some links:
In this example we simply write "Hello world!".
class HelloWorld {
        static function main() {
                var ts = new flash.text.TextFormat();
                ts.font = "Times New Roman"; // set the font
                ts.size = 12;                // set the font size
                ts.color=0xFF0000;           // set the color
                var p : flash.text.TextField = new flash.text.TextField();
                p.text = 'Hello world!';
                p.setTextFormat(ts);
                flash.Lib.current.addChild (p);
    }
}
To compile this file named HelloWorld.hx, create a file named compile.hxml and write in it:
-swf hello_world.swf
-swf-version 9
-main HelloWorld
-swf-header 100:30:40:ffffff
The last line specifies that the animation will be 100x30 pixels, that 40 frames per seconds will be used, and the background will be white. You then just need to type haxe compile.hxml in a shell or double click on compile.hxml if the extension *.hxml is associated to haxe. The file hello_world.swf is then created.
Let us draw a blue circle!
On the right the html code used to display this flash object is also shown.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
        width="50"
    height="50"
    align="middle">
<param name="movie" value="blue_circle.swf"/>
<param name="allowScriptAccess" value="always" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="salign" value="lt" />
<param name="bgcolor" value="#ffffff"/>
<embed src="blue_circle.swf"
       bgcolor="#ffffff"
       width="50"
       height="50"
       name="haxe"
       quality="high"
       align="middle"
       allowScriptAccess="always"
       type="application/x-shockwave-flash"
       pluginspage="http://www.macromedia.com/go/getflashplayer"
/>
</object>
Here is the haxe code used to generate blue_circle.swf:
import flash.display.Sprite;
import flash.display.MovieClip;

class BlueCircle extends MovieClip {
        
        public function new() {
                super();                               
                var circle1:Sprite = new Sprite();
                circle1.graphics.beginFill(0x0000FF);
                circle1.graphics.drawCircle(10, 10, 10);
                addChild(circle1);
        }
	
        static function main() {   
                var circle1:BlueCircle = new BlueCircle();
                flash.Lib.current.addChild(circle1);
        }
}
The circle has its center at (10,10) and a radius of 10 (this is the drawCircle(10, 10, 10) call). But actual size on the screen is also given with by the html sequence. If we double width and height, then the flash application appears to be scaled by two:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
        width="100"
    height="100"
    align="middle">
<param name="movie" value="blue_circle.swf"/>
<param name="allowScriptAccess" value="always" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="salign" value="lt" />
<param name="bgcolor" value="#ffffff"/>
<embed src="blue_circle.swf"
       bgcolor="#ffffff"
       width="100"
       height="100"
       name="haxe"
       quality="high"
       align="middle"
       allowScriptAccess="always"
       type="application/x-shockwave-flash"
       pluginspage="http://www.macromedia.com/go/getflashplayer"
/>
</object>
Put your mouse over the square:

The following code re uses the previous BlueCircle class to let the user draw dots.
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;

class MovieClipTest extends MovieClip {
        
        public function new() {
                super();
                var r:Sprite = new Sprite();
                r.graphics.beginFill(0xaaaaff);
                r.graphics.drawRect(0,0,200,200);
                addChild(r);
        }
        
        function AddCircle(e:MouseEvent) {
                var newCircle:BlueCircle = new BlueCircle();
                addChild(newCircle);
                newCircle.x = mouseX;
                newCircle.y = mouseY;
        }
        
        static function main() {
                var m:MovieClipTest = new MovieClipTest();
                m.addEventListener(MouseEvent.MOUSE_MOVE,m.AddCircle);
                flash.Lib.current.addChild(m);
        }
}
A moving square
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;

class MovingRect extends MovieClip {
        var r : Sprite;
        var x_ori:Int;
        var y_ori:Int;
        var theta:Float;
        var cos_theta:Float;
        var sin_theta:Float;
        
        public function new() {
                super();
                x_ori=50;
                y_ori=50;
                theta =0.01;
                cos_theta = Math.cos(theta);
                sin_theta = Math.sin(theta);
                var background:Sprite = new Sprite ();
                background.graphics.beginFill(0xffaaaa);
                background.graphics.drawRect(0,0,200,200);
                addChild(background);
                
                r = new Sprite();
                r.graphics.beginFill(0xaaaaff);
                r.graphics.drawRect(40,40,20,20);
                addChild(r);
                r.addEventListener("enterFrame",move);
        }
        
        function move(e:Event) {
                var new_x = cos_theta * (e.target.x-x_ori) - sin_theta *(e.target.y-y_ori);
                var new_y = sin_theta * (e.target.x-x_ori) + cos_theta *(e.target.y-y_ori);
                e.target.x = new_x+x_ori;
                e.target.y = new_y+y_ori;
        }
        
        static function main() {
                var m:MovingRect = new MovingRect();
                flash.Lib.current.addChild(m);
        }
}
Scaling, translating and alpha-ing
import flash.display.MovieClip;
import flash.display.Sprite;

class ScaleTrans extends MovieClip {
        public function new () {
                super();
                var r:Sprite = new Sprite();
                r.graphics.beginFill(0xaaaaff);
                r.graphics.drawRect(0,0,300,200);
                addChild(r);
                
                var i=0;
                var imax=10;
                for (i in 0...imax) {
                        var m=new MovieClip();
                        var p : flash.text.TextField = new flash.text.TextField();
                        p.text="Hello world!";
                        var ts = new flash.text.TextFormat();
                        ts.color=0xFF0000;
                        ts.size=20;
                        ts.font = "Verdana";
                        p.setTextFormat(ts);
                        p.scaleX=2;
                        p.alpha=1-i/imax;
                        m.addChild(p);
                        var circle2:Sprite = new Sprite();
                        circle2.graphics.beginFill(0xFFCC00);
                        circle2.graphics.drawCircle(120, 40, 40);
                        m.addChild(circle2);
                        m.x=10*i;
                        m.y=10*i;
                        m.alpha=1-(i+1)/imax;
                        m.scaleX=(i+1)/imax;
                        m.scaleY=(i+1)/imax;
                        addChild(m);
                }
        }
        public static function main() {
                var m:ScaleTrans = new ScaleTrans();
                flash.Lib.current.addChild(m);
        }
}
Link with the browser.
Click on the square below.

In this example we show how to call java script or how to link to a new url in the same window ("_top" represents the same window):
class Navigate extends flash.display.MovieClip {
        public function new () {
                super();
                var r = new flash.display.Sprite();
                r.graphics.beginFill(0xaaaaff);
                r.graphics.drawRect(0,0,200,200);
                addChild(r);
        }
        public function goToUrl(e:flash.events.MouseEvent): Void {
                try {
                        // execute some javascript
                        flash.Lib.getURL(new flash.net.URLRequest("javascript:alert('hi');"),"_top");
                        // link to url www.haxe.org
                        //flash.Lib.getURL(new flash.net.URLRequest("http://www.haxe.org"),"_top");
                        trace("Click!");
                } catch (e:Dynamic) {
                        trace (e);
                }
        }
        public static function main() {
                var m:Navigate = new Navigate();
                m.addEventListener(flash.events.MouseEvent.CLICK,m.goToUrl);
                flash.Lib.current.addChild(m);
        }
}
Combining all this together
First we have an animation of the letters, and then they are clickable.

We combine all the previous features in the two following classes MovingLetter.hx and AnimateTo.hx.
fabrice@derepas.com Fabrice Derepas's Home Page