PROWNE
PROWNE

Reputation: 39

How to convert PNG image to Array?

I'm trying to make a tile-based AS3 game that uses PNG images as the base for maps that loads them from the library and converts the data to an array with each pixel of data being an individual tile. Essentially, if I had a 128x128 pixel PNG with say, green pixels being converted to "GRASS" in my array, I could then cycle through the array and add tiles to the map movieclip accordingly.

I've looked at the ByteArray class and I can't seem to decode the data into a usable format. If anyone has a solution to do this, please let me know.

Upvotes: 0

Views: 1118

Answers (1)

Adam Harte
Adam Harte

Reputation: 10530

The BitmapData class seems like what you are looking for. Make sure the images in your library have there own class name, and that it's base class is Bitmap (in properties), then create an instance of the image, and loop through its bitmap pixel data using getPixel.

Something like this:

var image:Bitmap = new MyBMP();
var bmd:BitmapData = image.bitmapData;

for(var y:int=0; y < bmd.height; ++y)
{
    for(var x:int=0; x < bmd.width; ++x)
    {
        var pixelValue:uint = bmd.getPixel(x, y);
        trace(pixelValue.toString(16));
        // Test if the pixelValue matches the colour you want.
    }           
}

Just a note, make sure to use getPixel32 if you are use alpha channel.

Upvotes: 3

Related Questions