Ginger Binger

Lessons in programming, learning, and life

AS3 Garbage Collection With Closures

| Comments

I was talking with Ido about memory usage in AS3, and we discovered a small weakness in the AVM’s garbage collector. If you use a functional style of programming with lots of anonymous or nested functions, here’s something to watch out for:

import flash.display.Sprite;

function foo():* {
    var sprite:Sprite = new Sprite();
    sprite.addEventListener(Event.ENTER_FRAME, tick);

    // this closure creates a reference to the outer scope, foo
    return function():void { }

function tick(event:Event):void {

var func:* = foo();
// sprite is never garbage collected, and continues to tick

sprite will not be garbage collected, even though it’s just a local variable of foo and no other references to it exist!


| Comments

My buddies Shawn Tanner and Michael Swain have released Skincraft, an awesome skin creator for Minecraft. Check it out on Newgrounds:


I helped out with the 3D display of the Minecraft character, as well as the file sharing features of the Newgrounds API that allow people to upload their skins. I’ve been hard at work on the Newgrounds API for several months, and I think this is one of the best examples of it yet!

Happy 20th Birthday, Street Fighter II

| Comments

I don’t have many memories from when I was very young, but one sticks out clearly in my mind. My mom was shopping at the mall, with my brother and me in tow. As we walked past the Hills department store, a blast of electronic noise caught my ear. An arcade had opened in the mall, and like moths to flame, my brother and I stormed into it. To a kid, no experience is quite as visceral as walking into an arcade. And the first thing I saw was this:

AS3 Mouse Wheel Trouble

| Comments

If you’ve ever tried to add mouse wheel support to a Flash web app, it’s likely you’ve run into this problem: Scrolling the mouse wheel will scroll the web page, regardless of any mouse wheel listeners or code you have inside the Flash. There seems to be no way to easily prevent the web page from scrolling. Here’s an example.

This is a regression from AS2. In AS2, adding a mouse wheel listener would prevent the web page from scrolling. Not so in AS3. Even preventDefault) doesn’t seem to help.

Julia Sets on the GPU With Molehill

| Comments

Everyone has been buzzing about Molehill, the new low-level 3D API that will be available in Flash Player 11. This API finally gives Flash developers the GPU acceleration that they’ve been waiting for. It’s a wrapper over OpenGL/DirectX, giving you a nice interface with a OpenGL ES 2.0 featureset. That’s right, all the Z-buffered triangles and shaders your heart desires. :)

Adobe released a public beta of Flash Player 11, so I grabbed it and tried to get my Pixel Bender Julia set plotter running in the player. Unfortunately, Adobe hasn’t yet released the latest Pixel Bender Toolkit, which you would use to compile shaders in Adobe’s shader format for use with Molehill. But then HxSL appeared, from the brilliant Nicolas Cannasse – a shader compiler released before Adobe’s official tools!

ActionScript 3 Version Checking

| Comments

The Flash Player makes a decent effort to be forward-compatible. When you run an Flash Player 10 targeted SWF in Flash Player 9, it’ll run fine until you start using Flash Player 10 specific features. At best, the code will just fail silently or pop up an error dialog. But at worst, the file will fail to play at all! Here are some examples of features that will cause errors if the user is running an old version:

  • 3D transformations.
  • The Vector class.
  • Dynamic audio using SampleDataEvent.
  • Global error handling.
  • Multitouch and accelerometer events.

Even seemingly innocuous code can cause problems, such as Event.ADDED_TO_STAGE—it was first added in Flash Player 9 Revision 1! Therefore, it’s very important to check that the user has the Flash Player version that we expect. Otherwise, you may get some nasty reviews stating that your game refuses to work. :)

ActionScript 3.0 Events: The Myth of useWeakReference

| Comments

Memory management is tricky when working with a garbage collector. Throw in a complex display list hierarchy with lots of event handlers, and it’s definitely a challenge! It’s very easy to overlook some hidden object reference, causing objects to not be marked as garbage. Soon your memory usage is spiraling out of control! This has lead many developers such as Grant Skinner and Ted Patrick to suggest always setting useWeakReference to true when you call addEventListener.

This is indeed a good idea and can be a helpful failsafe, but I think that it fails to emphasize another important action: Always remove your event listeners. I fear that many developers may be given a false sense of security by always using weakly referenced listeners, so I’ve tried to create a clear description of how event listeners create references and how to clean them up. This assumes you have an idea of how Flash’s mark-sweep garbage collector works. If not, check out Grant’s excellent AS3 Resource Management articles, which are a must read for any Flash developer.

Doom, Heretic, and Hexen in Flash

| Comments

This project was originally posted on my blog on December 4, 2008.

Near the end of 2008, Adobe released Alchemy, a toolset to cross-compile C++ into ActionScript libraries. Naturally, the first thing that popped into my mind was to try to port Doom!

Doom running in Flash

This port is based on the vanilla Doom source code for Linux. A few months later, I added Heretic and Hexen support using the original DOS source of Heretic and Hexen. These sources were released by id Software and Raven Software under the GPL. The (messy) code for my port is also released under the GPL, available on GitHub.

These type of reverse-engineering projects are always fun. It’s a blast to look through the source of an ancient game! And it’s truly an experience when the game suddenly springs to life after a late-night hacking session. It was such a trip when I first saw “R_Init: Init DOOM refresh daemon” in my Flash output window… :)

If I were to start again, I’d probably base it on a more modern source port like PrBoom. That way I’d gain many of the bug fixes and cool features that these ports have solved years ago!

This port did manage to win FHM’s 2009 Web Game Awards! The amount of work I did to port such an awesome game was negligible, so I decided to donate the prize to charity. Go open source!

ActionScript 3.0 Game Architecture

| Comments

I had the pleasure of giving a talk on ActionScript 3.0 game programming at Flash Camp Philadelphia 2009. I spoke about how games are built, the pros and cons of the standard game inheritance hierarchy, and a few alternatives to it. I also talked about some techniques such as:

  • Game loop timing using getTimer()
  • Decoupling game logic and animation from Flash Player’s frame rate
  • Rolling your own display list for benefits such as depth sorting
  • Using the Flex Builder profiler to optimize your game

A lot of these techniques are used by the newer game frameworks such as Flixel, FlashPunk, and PushButton Engine, so it’s good to have an understanding of what’s going on behind the scenes.

This talk was given on November 7, 2009 at Philadelphia University. You can download the slides and some sample code here!