True one-file Haxe preloader using mxmlc
With combining the MochiAd sample as3+mxmlc preloader with a custom flash9 Haxe swf, I created nicely preloaded Haxe content. Furthermore, calling functions back and forth between the wrapper and content is also done. Continue reading for the details!(Edit: Don't forget to check out our latest article Preloading with Haxe and SamHaxe on the topic!)
We will need Haxe and the Adobe Flex 3 SDK. Download the latter for free if you do not have it yet, then add its bin directory to the PATH so that the mxmlc command can be found. Minimnal AS3 magic will be required, but Haxe/flash9 users should see nothing unfamiliar.
Download & Build
Next, get this package with the source code: http://www.mindless-labs.com/files/preloader/haxe_mxmlc_preload.zip. If you can't wait, jump to reading the source code directly - it is well commented.
Just execute make clean && make in the main directory to compile WrappedHaxe.swf, or if you do not have make, simply observe the Makefile for what to do (short: delete swf files, haxe build.hxml in hxswf dir, then mxmlc with the throng of parameters in the main dir).
Overview
What happens here? We have three classes: Preloader in Preloader.as is the preloader, Test in Test.as is the Wrapped AS3 content, and Main in Main.hx is the Haxe content. The Main class will be included inside the Test class using an mxmlc [Embed] tag. The preloader will wait for the whole swf to load, displaying some nice progress bar meanwhile, and once ready, create an instance of Test and add it as child.
The trick
What is the catch? The preloader has to be on the first frame, and Test has to be on the second, so that the preloader can execute as soon as its frame is loaded. The trick is that we cannot directly reference Test.as in Preloader.as, because then the compiler would include the whole Test class code on frame 1, and preloading would have no sense. So Test will be loaded by name from Preloader using the getDefinitionByName call, which is like Type.resolveClass in Haxe.
Second part of the trick: If we do not include a direct reference to the Test class in the Preloader, the mxmlc compiler won't compile Test into the resulting swf at all, and we would get a nice error. The trick is the -frame FrameName,ClassToInclude mxmlc compiler option, which I don't know how did the Mochi guys know to exist, because I couldn't find this in any mxmlc documentation (or did not see deep enough). This option adds ClassToInclude on a new frame called FrameName. So, by adding the -frame Test,Test option to mxmlc, a new frame labelled Test will be added to the resulting swf containing our wrapped code, and everybody is happy.
About MochiAds
What is MochiAds? This is a little promo here for a cool service. MochiAds is a way of making money from your games by putting ads in your game. The most used format is the ad in the preloader, you must have seen it in flash games. Currently, revenue per thousand views ranges approximately between $0.41 and $2 depending on many factors. You don't have to pay a cent, they pay with paypal or check. Sign up free here if interested:
Useful to know!
Keep these practical issues in mind when using the preloader:
Use the debug flashplayer. It will save you hours on misterious bugs.
Postpone Display chain acces to after Main constructor. The Haxe content may be added on the display list only after it has been constructed. This means you cannot access Boot's parent, or the stage. Best way is to bind a handler for ADDED_TO_STAGE in the constructor, and do all initalization there.
Embed tag uses a Loader. Beware! When instantiating an embedded class in AS3, mxmlc uses a Loader behind the scenes which which loads the content with loadBytes asynchronously, so the haxe object may not be ready for access yet. If you want to call any functions on the haxe content, be sure to check the loader status. Alternatively, you can call back from Haxe to the wrapper to sign that it is ready for communication. This is done in the source code, see the comments there.
Chain of object parentship. From the Test class to your Haxe Boot class the chain of objects is like this:
Test object -> HxInst object -> Loader object -> Haxe Boot object. In this example, I subclassed Main from Sprite and added
it to the Haxe Boot as first child, but that is not strictly necessary.
Preloader progress bar is now MochiAd's. If don't use MochiAd or you would like custom loading (draw an own progress bar, pie, etc), then use the ratio root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal to show progress. Update the progress in an ENTER_FRAME handler. Once bytesLoaded == bytesTotal, execute something similar as in opts.ad_finished.
Movie dimension: 2 (3) places to modify. It is nice to make the preloader and haxe content dimension the same. You have to modify the mxmlc paramter in the Makefile, and the haxe compiler option in build.hxml. The third place is the MochiAd parameter in Preloader.as, if you use MochiAd pre-game ad.
Happy Haxing!
PS: I would like to see a pure Haxe preloader very soon!
Comments
Comment by [yfan], on 09.09.18. 15:41
It seems to be exacly what i’m looking for, but the download links don’t work. Can you fix it?
Cheers!
//hey! the site called me a spammer!
Comment by [ron], (URL) on 09.09.18. 17:42
Oh, thank you for noting. I will fix the files in the weekend. Meanwhile, you may want to check our new project SamHaxe – it has a demo called PreloaderDemo, which shows an easy was for doing a preloader in pure Haxe.
http://mindless-labs.com/trac/samhaxe
Sorry for the spam thing, it should be fixed :)
Robin
Comment by [ron], (URL) on 09.09.20. 09:16
Ok, file links are up back!
Comment by Dobos Bence, () (URL) on 09.09.25. 12:57
I’m using this a while. But with preloader it’s a pain to get params from html, which embed your swf (as preloader gets the params, and you have to give it to your haxe object). You want params, as Kongregate API uses them.
Comment by [ron], (URL) on 09.09.25. 14:48
Hi Bence, what is your setup? The one described here with mxmlc+embedded haxe object, or the newer one described in the recent SamHaxe article?
Either way I can think of a solution which might ease our lives with html parameters or flashvars..
Comment by Dobos Bence, () (URL) on 09.10.01. 11:58
I’m using mxmlc+embedded haxe as described in the article. I’m using callback to get the params from the preloader when the haxe object is ON_STAGE. I modified the Kongregate API haxe code to use this params.
Comment by Mk12, () on 09.11.28. 13:12
It doesn’t work when I put my game in it gives a null pointer (just after mochi ad finishes). I changed everything that is supposed to be..
Comment by [ron], (URL) on 09.11.28. 21:08
This was due to referencing stage prematurely. Be sure to postpone stage access until when the object is already on the display list.
Comment by Pieter Witvoet, (URL) on 10.01.26. 18:39
Recently I had to build a preloader for a haXe game I’ve been working on, and whatever I tried, nothing really worked (including this approach, mxmlc didn’t like my assets .swf for some reason). However, I figured out a way to do a pure haXe preloader: http://createivity.wordpress.com/2010/01..

