Using AS3 project from Haxe as library asset

12.21.2007 6872 7

This post is an extension of this one talking about flex in haxe without external loading.
When you want to use a SWF made with the Adobe Flash 9 or with the free mxmlc compiler, you have several ways in Haxe.
The easy one is to load this SWF as external data. And then working with the .hx header classes generated from the AS3 project.
But if we want have only one SWF output file, we have to "merge" the 2 projects.
Here come a weired way to do that but it's the only one I've found

To do that, we need first make a SWF with the mxmlc compiler.
This SWF will be used as simple swf-library, while compiling with Haxe... but for some dark reasons, this one kills all Haxe initializations ! (no static main() called, almost nothing done...)
So, the trick is to call our Haxe class entry point inside the AS3 code : :

    import flash.display.*; 
    import flash.text.*;
    import flash.utils.getDefinitionByName;
    public class AS3Project extends Sprite
        public static var instance : AS3Project;
        public function AS3Project()
            var tf : TextField = new TextField();
            tf.y = 20;
            tf.text = "Hello from AS3Project";
            this.addChild( tf );
            // It call the Main Haxe class from here and set the pointer [instance]
            instance = this;
            var MainHxClass : Class = getDefinitionByName( "HxProject" ) as Class;
                        var instanceMainHx : Object = new MainHxClass();
        public function Foo() : void
            var tf : TextField = new TextField();
            tf.y = 40;
            tf.text = "Hello from Foo";
            this.addChild( tf );

As we can see we instanciate Haxe project from AS3...
Then let create Haxe extern classes from the AS3 project :

haxe --gen-hx-classes AS3Project.swf

It creates a directory called hxclasses with all classes used into our AS3 project (extern classes are only the headers). So we can acces them from our Haxe project
HxProject.hx :

class HxProject
    static function new()
        var tf = new flash.text.TextField();
        tf.text = "Hello from Haxe";
        flash.Lib.current.addChild( tf );
    static function __initAS3()
        var mc = new flash.display.MovieClip();
        AS3Project.instance.parent.addChild( mc );
        flash.Lib.current = mc;

We don't need to call any main from Haxe because it's done from AS3.
__initAS3 is here to do a quick Haxe initialization, but some function from Boot like trace will never be available
The compiler commands look like:

haxe HxProject -swf finalPorject.swf -swf-lib AS3Project.swf -swf-version 9 -cp hxclasses

I hope I could delete this post soon, when the process will be improved in Haxe


01.29.2008 at 21:40 daniel

Hi, this sounds very cool but when I execute it, it gives an error on this line:
AS3Project.instance.parent.addChild( mc );
AS3Project variable is not defined.
what might be the cause?
thx, regards

01.29.2008 at 22:18 Michal

Hi Daniel!
Maybe you forgot generating Haxe headers from the AS3 project like that :
haxe --gen-hx-classes AS3Project.swf
It makes the extern class AS3Project that is used by the HxProject, and the AS3Project is enabled doing that.
Hope it helps,

01.30.2008 at 00:46 daniel

thx, for a hint but I did so. I excute first:

haxe --gen-hx-classes AS3Project.swf

then I use:

haxe HxProject
-cp hxclasses
-swf final.swf
-swf-lib AS3Project.swf
-swf-version 9

to compile the project. When I omit -cp swith it gives an error at the compilation, but when I use it the hexe class cannot access to the flex2 one.
I can execute any method of the haxe class from flex though. Any ideas? Maybe there is some issue with initiation times?

02.07.2008 at 12:43 daniel

Just in case someone has a similar problem:
it seems haxe sometimes is generating incorrect extern classes when using --gen-hx-classes
A quick fix was to import the class the extern class is extending in the same file as the extern class.
this got rid of some of the runtime errors, but I still couldn't get it to work with haxe 1.17

02.07.2008 at 14:46 Michal

Hi !
I have no much time at the moment, but I'll check what's going on soon

02.09.2008 at 13:39 Michal

Hej !
It's actually a bug in 1.17, Haxe "extern" classes were erasing existing AS3 classes.
It have been fixed in the CVS version, at the time I write that.
Comming soon in next release I hope

02.10.2008 at 12:23 daniel

ok, good to know! when you have some experience and you have it done previously it must be not so difficult to detect. but when you start it's quite confusing when things that are supposed to work just don't.
thx for all comments.

Write a comment