I thought I’d expand a little on the al jazari flash game, and how to develop flash games using free software.
Haxe is really rather nice, and although I’d prefer something with more parentheses (and ironically, less static typing) it does make programming for flash a nicer experience than I’d been led to believe is normally the case. I only used haxe, gimp and a bit of fluxus to get sprite renders of the 3D models, along with the firefox and of course its flash plugin (I’d like to use gnash if I do a lot more of this). I’m going to describe the basics and some of the things it took me longer to figure out. I relied a lot on howtos in blog posts, so I thought it would be a good idea to join in the fun.
Firstly you need a file called compile.hxml with something like this in it:
-swf al-jazari.swf
-swf-version 9
-swf-lib resources.swf
-main AlJazari
-swf-header 640:480:40:ffffff
This is something like a makefile for haxe and contains the main output file, the main class and the size of the area the plugin will take up. You compile your haxe script with the command haxe compile.hxml.
The style of haxe (or paradigm, if you will) is very Java like (which is probably good for me after all this exposure to Scheme and C++). You need to name your file the same as the class containing the main function eg:
class MyMainClass
{
static function main()
{
trace("this gets run");
}
}
Will work if it’s called MyMainClass.hx and the compile.hxml contains:
-main MyMainClass
You can then test it out by writing a little html like this:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="50"
height="50"
align="middle">
<param name="movie" value="al-jazari.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="al-jazari.swf"
bgcolor="#000000"
width="640"
height="480"
name="haxe"
quality="high"
align="middle"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
/>
</object>
And then point your browser at this to test the code.
Using textures
The compile.hxml file for al jazari also includes a reference to a lib – which is where you can embed resources like textures for making sprites. You build one of these with a bit of xml like this:
<?xml version="1.0" encoding="utf-8"?>
<movie version="9">
<background color="#ffffff"/>
<frame>
<library>
<bitmap id="BlueCubeTex" import="textures/blue-cube.png"/>
</library>
</frame>
</movie>
The id refers to a class you have to add to your haxe script – I think this is like a forward declaration or extern of some form, which allows you to refer to your texture:
class BlueCubeTex extends BitmapData { public function new() { super(0,0); } }
This bit of code (say in a class inherited from Sprite) will then draw the texture like this:
graphics.clear();
graphics.beginBitmapFill(BlueCubeTex);
graphics.drawRect(0,0,64,64);
graphics.endFill();
The xml script is needed to build the swf library file which contains the textures, which you do by running:
swfmill simple resources.xml resources.swf
swfmill is free software, and installable with apt-get install swfmill on ubuntu.
Using sound
I couldn’t figure out a way to embed sounds using swfmill, it seems that it’s a recent feature, and I couldn’t find any examples that included the haxe code to load them. I did get this to work though:
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundLoaderContext;
…
var sound: Sound = new Sound();
var req:URLRequest = new URLRequest(“path/from/main/swf/to/samples/sample.mp3”);
var context:SoundLoaderContext = new SoundLoaderContext(8000,true);
sound.load(req,context);
Which loads mp3s from a url, and then after some time (you can set up a callback to tell you then it’s loaded but I didn’t bother):
sound.play(0);
Where the parameter is the offset (in samples I think) from the start of the sound.