This tutorial is a work in progress.
In my PlayState base class (which inherits or extends FlxState), I import one extra library:
import flash.display.BitmapData;
Now I can work directly with the bitmapData of an embedded image. I add one function that accepts a BitmapData object and builds blocks in the world. It uses a crude form of RLE (Run-Length Encoding) to reduce the total number of blocks used.
public function loadLevelDataFromBitmapData(b:BitmapData):void
{
var w_offset:uint = 0;
var h_offset:uint = 0;
var rle:Array = [0, 0];
for (var h:int = 0; h < b.height; ++h)
{
for (var w:int = 0; w < b.width; ++w)
{
var val:uint = b.getPixel(w,h)
if ( val != rle[0] || w == b.width-1 )
{
switch( rle[0] )
{
case 0: // solid block
//FlxG.log('block: ' + [rle[0], w_offset, h_offset, rle[1]*8, 8] );
_blocks.add(this.add( new FlxBlock(w_offset, h_offset+1, rle[1]*8, 8, ImgTech)));
break;
case 10109983: // brown dirt
_blocks.add(this.add( new FlxBlock(w_offset, h_offset+1, rle[1]*8, 8, ImgDirt)));
break;
}
w_offset += rle[1]*8;
rle[0] = val;
rle[1] = 1;
}
else
{
rle[1] += 1;
}
}
rle[0] = 0;
rle[1] = 0;
w_offset = 0;
h_offset += 8;
}
FlxG.log("converted");
}
To generate the blocks for a level, I call the function in the level's initializer:
loadLevelDataFromBitmapData((new ImgLevel).bitmapData);
Here, ImgLevel is an embedded png. I used The Gimp to create a png that's 80 x 80, with white meaning no blocks and black meaning a solid normal block.
Known Issues:
For some reason, 80 pixels in the image * 8 pixel height offset doesn't seem to add up to 640 high.