More on haxe development

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.

2 thoughts on “More on haxe development

  1. Hi Dave,

    I’m glad you find swfmill useful. You may be interested to hear that I have just released swfmill 0.3.0 today.

    Unfortunately swfmill was not maintained for a long time, and so until today the latest official release (0.2.12) was over two and a half years old. Many useful features have only been available via unofficial builds, or to those willing to roll their own.

    I believe that it was possible to successfully import MP3s using 0.2.12, but that version certainly lacks many features that are available with the new release. In particular 0.2.12 did not support a very wide range of sample rates, so perhaps you’re using one of the ones that version did not support.

    You should be aware that 0.3.0 includes one breaking change, which affects SVG import. It sounds like you are not using SVG import, so that change should not affect you. The pain is worth it, anyway, because 0.2.12 only supported importing one SVG per SWF — not great :).

    I urge you to give swfmill 0.3.0 a try. It’s available in source form from the official website at http://www.swfmill.org/. It’s a bit tricky to compile, and is still rather poorly documented, but you are very welcome to ask for help on the mailing list, and the other readers and I will do our best to help.

    Cheers,
    Dan.

  2. Hi Dan,

    Thanks for the feedback! I’ll certainly start using the new version. SVG import would certainly be very useful for me too.

    One reason I didn’t try mp3 embedding was that I’m very new to this and I couldn’t find any example code in haxe that showed how it worked from that end.

    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *