PDA

View Full Version : Pixel Animation



foomonger
January 30th, 2006, 01:07 AM
/*
www.foomonger.com
Pixel Animation
The following code draws a series of frames that are represented by an array of numbers.
*/

// 2d array, aka an array of frames;
// the binary of each number represents the pixels to draw for a row;
var frames_array:Array = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,64,0,0,0,0,0,0],[0,0,0,68,0,0,0,0,0,0],[0,0,0,68,0,0,128,0,0,0],[0,0,0,68,0,0,128,64,0,0],[0,0,0,68,0,0,128,96,0,0],[0,0,0,68,0,0,128,112,0,0],[0,0,0,68,0,0,128,120,0,0],[0,0,0,68,0,0,128,124,0,0],[0,0,0,68,0,0,130,124,0,0],[0,0,8,0,32,0,130,124,0,0],[0,0,16,0,16,0,130,124,0,0],[0,0,32,0,8,0,130,124,0,0],[0,0,0,68,0,0,130,124,0,0],[512,512,512,580,512,512,642,636,512,512],[256,256,256,324,256,256,386,380,256,256],[128,128,128,196,128,128,130,252,128,128],[64,64,64,68,64,64,66,124,64,64],[32,32,32,36,32,32,34,60,32,32],[16,16,16,20,16,16,18,28,16,16],[8,8,8,12,8,8,10,12,8,8],[4,4,4,4,4,4,6,4,4,4],[2,2,2,2,2,2,2,2,2,2],[1,1,1,1,1,1,1,1,1,1]];
var framesIndex:Number = 0; // index counter

setInterval(runAnimation, 100); // run the interval

// interval function
function runAnimation():Void {
_root.clear();
framesIndex = (framesIndex < frames_array.length) ? framesIndex : 0;
drawPixelGrid(frames_array[framesIndex++], _root, 0, 0);
}

// draw the given pixel grid array
function drawPixelGrid(pixelGrid:Array, target:MovieClip, x:Number, y:Number):Void {
for (var i:Number = 0; i < pixelGrid.length; i++) {
drawPixelRow(pixelGrid[i], pixelGrid.length, target, i, x, y);
}
}

// draw a row pixels from the binary of a given number
function drawPixelRow(dec:Number, bytes:Number, target:Object, row:Number, x:Number, y:Number):Void {
// check if each bit
for (var i:Number = 0; i < bytes; i++) {
// ** draw pixel if bit = 1
if ((dec & ((Math.pow(2, bytes - 1)) >> i)) > 0) {
drawPixel(target, (x + i), (y + row));
}
}
}

// draw a pixel; numbers inflated for visibility
function drawPixel(target:Object, x:Number, y:Number) {
target.moveTo(x * 5, y * 5);
target.beginFill(0x000000, 100);
target.lineTo((x + 1) * 5, (y * 5));
target.lineTo((x + 1) * 5, (y + 1) *5);
target.lineTo(x * 5, (y + 1) * 5);
target.lineTo(x * 5, y * 5);
target.endFill();
}

hybrid101
January 30th, 2006, 01:18 AM
it's a bit boring and repetitive, seriously... why not try making a longer animation? it's looking good for now

foomonger
January 30th, 2006, 01:36 AM
(I forgot to upload the pixel grid generator I used to make the frames.)

Yeah, I know. I'm lazy and not as creative as I was back in school. Bad combination. If I had a faster way of generating the pixel data maybe.

kdd
January 30th, 2006, 01:46 AM
btw, i don't think you need documentation. :P

hybrid101
January 30th, 2006, 02:16 AM
oohh...tile based generator. cool!