Shide and Prame


Using Binary Data in Actionscript 3

This is for anyone who’s having hair-tearing issues with working with binary data in Flash or Flex, or for those who want to be working with it.

Firstly, embedding binary data

How to embed? Check this article on embedding binary data at For those in a hurry, this is what I took away from it:

       mimeType="<span style="color: #3366ff;">application/octet-stream</span>")]
private static var __PuzzleData:Class; // This class object constructs a ByteArray subclass
protected static var data:ByteArray = new __PuzzleData(); // statically constructs an instance

// Example usage of the embedded byte array
public function getPuzzle(puzzleNumber:int):void {
  // set the data ByteArray to the start of the puzzle
  data.position = puzzleNumber * PUZZLE_BYTE_SIZE;
  // let the puzzle stream its data from the byte array using read*() calls
  var puzzle:Puzzle = new Puzzle(data);
  return puzzle;

Endianness in Flash

There’s one more thing you might need to take special notice of if you’re reading multi-byte numbers, which most numbers are. That is to note which Endian the data is formatted in.

The Endianness of a number is simply the order that its bytes are recorded in the file (or transmitted over the network). As far as I understand it, most compiled programs running on x86 hardware that write numbers to a file do so in “Little Endian” format, which means they write the least significant bytes first. Flash’s default Endianness is “Big Endian”, which expects the most significant bytes first, because that’s the standard endianness for network transmission.

So if your data file seems corrupted when you read it in Flash/Flex, this is very possibly the reason. Setting the Endianness is done on a per-ByteArray level:

myFancyByteArray.endian = Endian.LITTLE_ENDIAN; // set the reads/writes to use little endian

HaXe Fun

PixelLake screenshot

Check out my first visual experiment with HaXe, an excellent new(ish) programming language that can compile to swf!

addEventListener() Is the New Malloc()

Remember back when you learned C? Way, way back in time? Well I do. When I recall my experience with C, I remember it being intoxicatingly fast and powerful. Kinda felt like mainlining into an artery of the machine: so much potential for incredible speed, but also horrendously dangerous and unforgiving.

Saving Time When Dynamically Constructing BitmapData Subclasses Created in Flash

When constructing custom BitmapData subclasses which have been defined in the Flash Studio (such as MyFancyBitmapData), it turns out that you don’t actually need to pass the correct width and height of the bitmap. The MyFancyBitmapData class completely ignores its two (required) constructor parameters, and uses hard coded ones.

So don’t bother meticulously violating DRY principles when creating these things dynamically. You can pass any integers you like, including zero or negative one, but I particularly like using NaN because it makes it clear that you’re not even trying to supply a number that could possibly be interpreted in any meaningful way (even if that’s a tad passive aggressive…)

// e.g.
var myBitmapData:BitmapData = new MyFancyBitmap(NaN,NaN);
trace(myBitmapData.width + ", " + myBitmapData.height); // outputs: 45, 45

It seems a bit ugly to me that the generated BitmapData subclasses require their parameters. I really do think they should be optional at the very least, and ideally, illegal.

Hope this saves someone some time.

Catmull-Rom Curve

Ever wanted to draw a curve directly through any number of points with the Flash Graphics class?

Here’s a class that will do that for you. It uses a Catmull-Rom spline (named after its inventors), which draws directly through any number of points.