A week of Haxe

It’s been a long time since I used Haxe — my last commit to the Iridescence repository was over six years ago, and I haven’t been back to it ever since. When Flash finally reached its end-of-life and my old Flash games suddenly became unplayable, I realized that this would be a perfect opportunity to get back to it, see what had changed, and do a bit of digital preservation work while I was at it.

Since the majority of my old games were written in Flashpunk, I figured the logical first step would be to check into Haxepunk, a somewhat well-known port/enhancement. For some reason I had never had any luck getting Haxepunk to work in the past — it would always launch a totally dead app that didn’t even seem to run my main method, but who knows — maybe things had changed.

As it turned out, the Haxepunk project has been dead for some time. The forums are overrun with spam, and the official Twitter account hasn’t tweeted in nearly three years. Downloading the library just to try it out confirmed my fears — OpenFL had long-since moved on, leaving Haxepunk incompatible with the latest API.

By a stroke of luck I happened across a totally different Flashpunk port, this one called hxpunk, by an old regular in the community. I decided to use this for my first foray.

Gruepunk was the first game I ever made, back when I took Game Programming 101 at NHTI . In terms of actual gameplay there’s basically nothing going on — just a few buttons to click that all take you to the same ending. Its simplicity made it the perfect candidate for my first attempt at converting a game between two different languages, and the process went pretty smoothly — the major timesink was trying to elegantly fix some bugs in hxpunk when I should have just written an ugly hack from the beginning.

Seeing Gruepunk rendering in the browser, after only one day of work, was super encouraging. While some code-conversion tools for as3 to haxe do exist, I did this port entirely by hand after my first attempt at automated conversion caused friction I didn’t want to deal with.

Next up was Hypothermia, which I was sure would be a much bigger project. Not only did I have animations, particle effects, and audio to work with this time, but I had also written quite a lot of the gameplay code in a very crude early version of Slang, which I was not particularly looking forward to rewriting.

This time I decided to give code conversion a serious try. The “state of the art” in this area is a program called ax3, but it assumes that your entire classpath will be convertable to Haxe, which is not necessarily the case. I opted to use the as3hx instead, which is not quite as full-featured, but produces a straight syntax rewrite while leaving everything else up to the user, which is exactly what I wanted. There are a few broken points, like how it transforms all Dictionary access calls into a reflection get/set operation (which is not at all what you want), but once I figured out what to be careful for I was able to make good progress.

The first major problem came when I finally got the game running and found myself in the first scene — looking at flurries of snow being rendered at 9fps. Yep, hxpunk was a direct port of Flashpunk, and as such used the same bitmap-based renderer. Good snow (not to mention fire) is an absolutely essential part of the experience, and I knew I couldn’t achieve the emitter performance I needed with my current setup. Seemed like I was going to have to dig in and get Haxepeunk working.

I won’t get into the details here, but suffice it to say that my new branch of Haxepunk deleted a lot more code than it added. I dramatically simplified the project structure, updated the OpenFL/Lime code that had been broken in updates, and reverted some API changes that were causing me grief. It took some time, but by the end of the day the samples were actually working, and from there — barring some issues with Slang and my deserialization system, which I’ll talk about a little later — it was largely smooth sailing.

The last project of the week was Humphrey’s Tiny Adventure Remastered, which ended up being surprisingly smooth. Humphrey actually borrowed a few classes from Hypothermia directly, and its code objects were often nothing more than containers for a cutscene script (written in a more advanced version of Slang which I ported to Haxe a long time ago).

This ended up being the simplest port so far, with the majority of the work being done in a single day, with a few hours the next day to iron out some kinks and test thoroughly before uploading. As always, the data-driven approach was a massive boon. I had to tweak the files a bit to be compatible with the new version of Slang, but keeping such a substantial chunk of the game out of the code itself went a long way

Overall, this process was much smoother than it could have been, and the results were much better than I expected. I’m once again impressed by Haxe/OpenFL’s ability to target multiple platforms with very little fuss. This week was certainly not without its hardships, but the results are very promising. I’ll probably continue working on some of my other old games as well.

And if anyone reading this happens to have a flash game they’d like to see brought forward into the era of HTML5, feel free to get in touch!