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..

  
Remember personal info?

Emoticons / Textile

To prevent automated commentspam we require you to complete this silly task.
 

  (Register your username / Log in)

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.

About

Flash game development using HaXe and pals.

Archives

01 Nov - 30 Nov 2009
01 Oct - 31 Oct 2009
01 Sep - 30 Sep 2009
01 Jul - 31 Jul 2009
01 May - 31 May 2009
01 Apr - 30 Apr 2009
01 Mar - 31 Mar 2009
01 Feb - 28 Feb 2009
01 Jan - 31 Jan 2009
01 Dec - 31 Dec 2008
01 Nov - 30 Nov 2008
01 Oct - 31 Oct 2008
01 Sep - 30 Sep 2008
01 Aug - 31 Aug 2008
01 Sep - 30 Sep 2007

Calendar

« February 2010
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28            

Linkdump

Game design document - §

If you are about to create a new game, be sure to read this nice article along with its predecessors.

09.11.24. 13:29 | comment!

VelociRapid game element preview - §

Click the banner to play a preview of prey fleeing, and also take a peek underneath to see the quadtree space partitioning (nodes do not collapse right now). WSAD to control the dino.

09.11.19. 23:18 | comment!

Gambit's response to recent virtual currency changes - §

An interesting read at the Gambit blog.

09.11.05. 10:19 | comment!

Dirty coding tricks of game developers - §

Read it at Gamasutra

09.11.03. 10:02 | comment!

Payment providers - §

Some alternative payment providers for running your own virtual curreny system:

09.07.13. 21:38 | comment!

ssh tunneling in linux - §

To forward the local port X to the local port Y of the remote machine:

ssh -L X:127.0.0.1:Y remote.machine -N

Usage example: Setup a local web proxy (like privoxy) on a remote university machine, and connect to it from home. This way you can access university resources with ease.

09.05.14. 10:28 | comment!

Free flag images - §

http://www.33ff.com/flags/index.htm

09.04.08. 11:52 | comment!

Render text with ImageMagick - §

convert -font font.ttf -background none -geometry +0+0 -fill \#ffffff -pointsize 18 label:"`cat txt`" -set label '' out.png

09.04.02. 15:16 | comment!

Online Latex equation editor - §

Online latex is

09.03.11. 09:02 | comment!

Create swc from a tree of as3 files - §

V1 (thanks to Jarrad Hope!) compc -output my_swc.swc -include-sources .

V2 (the old and hacky :) cd tree_top; compc -source-path . -output my_swc.swc -include-classes `find . -regex .*as | awk '{gsub(/\.\//, "", $0); gsub(/\.as/, "", $0); print $0}'`

09.03.06. 09:58 | two comments

Delete first line from file - §

sed -i '1d' file.txt

09.02.18. 12:28 | comment!

Gentoo: check security holes - §

glsa-check -p $(glsa-check -t all)

09.02.04. 11:30 | comment!

Execution speedup with fifo - §

Speed up the execution of programs that generate massive unwanted log-files by sending those logs to null through a fifo: mkfifo logfile
cat logfile > /dev/null &
./myprogram -log logfile

09.01.19. 14:09 | comment!

ImageMagick PNG background and auto-crop - §

Set background to white and auto-crop with ImageMagick: for i in `ls *png`; do convert -flatten $i x.png; convert -trim x.png out_$i; done

09.01.12. 17:15 | comment!

Linux: split file on pattern - §

awk '/PATTERN/{i++}{print > "file.pdb."i}' file, found here

08.12.15. 17:46 | comment!

Replace in multiple files on Linux - §

perl -pi -w -e 's/search/replace/g;' *.php
Found here.

08.12.10. 10:42 | comment!

XPath in Python - §

A nice summary about options for xpath with python here.

08.12.09. 18:41 | comment!

Christmas Icons - §

Free Chrismtas Icons!

08.12.08. 11:46 | comment!

PS print on WinXP - §

Print to PS on WinXP without any printers! Cool :)

08.12.04. 12:59 | comment!

Sorry, OpenOffice - §

OpenOffice is simply a no-go. Ill UI, missing features. Go for Latex or Crossover Office instead.

08.12.02. 14:31 | comment!

Last Referrers

Miscellany

Powered by Pivot - 1.40.7: 'Dreadwind' 
XML: RSS Feed 
XML: Atom Feed