haXe preloader
January 15, 2010 at 11:20 pm | Posted in Flash games, Game development, HaXe, Programming | 6 CommentsTags: HaXe, preloader, swfmill
2 years ago I tried to create a preloader in haXe, but whatever I tried, nothing worked. Today, I had to create a preloader for a project at work. Needless to say, I wasn’t too happy, with that first experience in mind.
I did some Googling and found various forum and blog posts, but whatever I tried, nothing seemed to work. Mindless Labs suggested using mxmlc to embed the game .swf into an Actionscript preloader, but mxmlc couldn’t digest our game .swf. Game Haxe had another approach, but apparently that one broke when haXe 2.03 got released. Using the MochiAd preloader didn’t work out of the box either, because that one would only be run after the whole .swf was loaded. So I was thinking towards creating a separate preloader .swf that would load the main game .swf – it’s a messy, multi-file solution, but at least it’s a solution.
However, I talked about it to a coworker, asking how this would be done in ActionScript. The standard approach is to move all assets to a later frame, and to put some preloader code and assets in the first frame. I decided to give it a try – mind you, the same thing had failed me 2 years ago – and suprizingly, it worked! Back then, the assets had been inaccessible, but some haXe and swfmill versions later it works like a charm.
Here’s how my swfmill .xml file looks like now:
<?xml version="1.0" encoding="iso-8859-1" ?>
<movie version="9" width="600" height="400" framerate="30">
<background color="#FFFFFF"/>
<frame /> <!-- add one empty frame -->
<frame>
<library>
<clip id='image' import='images/image.png' />
<font name='font' import='fonts/font.ttf' />
</library>
</frame>
</movie>
Line 4 is important: it adds an empty frame, into which the haXe code is compiled. The second frame contains all the heavy-duty assets, which will be available once the whole .swf has been downloaded.
My main function now looks like this:
package ;
import flash.events.Event;
import flash.Lib;
import haxe.Log;
import game.Game;
class Main
{
static function main()
{
Log.trace("Starting preloader");
Lib.current.root.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
static function onEnterFrame(event : Event)
{
Log.trace("Downloaded " + Lib.current.root.loaderInfo.bytesLoaded + " of " + Lib.current.root.loaderInfo.bytesTotal + " bytes");
if (Lib.current.root.loaderInfo.bytesLoaded >= Lib.current.root.loaderInfo.bytesTotal)
{
Lib.current.root.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
Log.trace("Preloading finished");
// The Game class adds images to the screen, sets up listeners, etc.
var gameInstance : Game = new Game();
}
}
}Normally, I would’ve created a Game instance immediately within the main function, but the assets only become available once the whole .swf is loaded, so it’s a smart idea to wait until the number of bytes loaded matches the total number of bytes. ;)
I hope this post is useful to those who are looking for a haXe preloader. :)
<movie version=”9″ width=”600″ height=”400″ framerate=”30″>
<background color=”#FFFFFF”/>
<frame /> <!– add one empty frame –>
<frame>
<library>
<clip id=’image’ import=’images/image.png’ />
<font name=’font’ import=’fonts/font.ttf’ />
</library>
</frame>
</movie>
6 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a Reply
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
Yes, this method is also used by Mindless-Labs using SamHaXe (the preloading article after the MXMLC one). The tiny annoyances (well it’s not a problem per se) with this method is that only the game “assets” is in the 2nd frame. The whole game code is still on the 1st frame. I’d be interested myself at how big a flash code (minus the assets) since I tend to make preloader as small & light as possible, so user don’t have to wait too long to “see” something going.
Comment by aaulia— January 27, 2010 #
Thanks… I was searching for a simple solution like this in Haxe for quite sometime. This post really helped.
Comment by Arun— April 9, 2010 #
This example doesn’t work, nor does the idea. The library created by SwfMill isn’t available in haXe, even after wrapping each one in a class.
Nice effort though.
Comment by Dang— June 5, 2010 #
Well, it works for me… what exactly went wrong when you tried it? What versions of SwfMill and haXe are you working with?
Comment by Pieter Witvoet— June 6, 2010 #
[...] also found this sort of "half-preloader", it is only half in the sense that it only loads assets that is needed later. But in my game the [...]
Pingback by How to do a proper haXe preloader for Flash, or what do you think is the best way to do it? | DeveloperQuestion.com— October 11, 2010 #
[...] for the current version of HaXe. My preloader’s based on the general approach explained here: code on frame 1 of the .swf, resources on frame 2. The code for my game is 26KB, so it may be a [...]
Pingback by FLASH PRELOADER WITH HAXE 2.07 » randomnine— May 23, 2011 #