Something like this (with fading instead of gravity) would also work. Unfortunately, this example breaks an image up into single pixels, not larger blocks.
Printable View
Something like this (with fading instead of gravity) would also work. Unfortunately, this example breaks an image up into single pixels, not larger blocks.
Woohoo! Got it working 2 different ways. Hopefully someone out there will find this useful!
Code:import flash.display.BitmapData;
import flash.geom.Matrix;
// ADD A MOVIECLIP WITH THE INSTANCE NAME 'IMAGE' ONTO THE STAGE AND ENJOY!
// either draw the pieces into a canvas or just move them on the stage (less efficient but might be good enough for some cases)
var useCanvas:Boolean = true;
// size of each bitmap block
var size:uint = 2;
// array to reference all the parts and speeds
var pieces:Array = new Array();
// y coordinate where pieces should disappear
var yMax:uint = 400;
// copy and hide original movieclip
var imageData:BitmapData = new BitmapData(image.width, image.height);
imageData.draw(image);
image.visible = false;
// if just adding bitmaps to the stage
if (!useCanvas){
// make holder for bitmaps and place at position of original movieclip
var bitmapsHolder:Sprite = new Sprite();
bitmapsHolder.x = image.x;
bitmapsHolder.y = image.y;
addChild(bitmapsHolder);
// if drawing into a canvas
} else {
var canvasWidth:uint = 550;
var canvasHeight:uint = 400;
var canvas:Bitmap = new Bitmap(new BitmapData(canvasWidth, canvasHeight, true, 0));
addChild(canvas);
}
// slice up clone of movieclip
for (var i:int = 0; i < image.width; i += size) {
for (var j:int = 0; j < image.height; j += size) {
var bitmapData:BitmapData = new BitmapData(size, size, true, 0x00000000);
bitmapData.copyPixels(imageData, new Rectangle(i, j, size, size), new Point(0, 0));
var bitmap:Bitmap = new Bitmap(bitmapData);
var obj:Object = new Object();
obj.x = i;
obj.y = j;
obj.xSpeed = 5 - Math.round(Math.random() * 10);
obj.ySpeed = 5 - Math.round(Math.random() * 10);
// if not using a canvas
if (!useCanvas){
// save bitmap to object
obj.bitmap = bitmap;
pieces.push(obj);
// add bitmaps to stage
bitmap.x = i;
bitmap.y = j;
bitmapsHolder.addChild(bitmap);
// if using a canvas
} else {
// save bitmapdata to object
obj.bitmapData = bitmapData;
// adjust coordinates based on original image position (based on canvas at 0,0)
obj.x += image.x;
obj.y += image.y;
pieces.push(obj);
// copy bitmapdata to canvas
canvas.bitmapData.copyPixels(obj.bitmapData, new Rectangle(0, 0, size, size), new Point(obj.x, obj.y));
}
}
}
trace("pieces:", pieces.length);
// animate
if (!useCanvas){
addEventListener(Event.ENTER_FRAME, animateOnStage);
} else {
addEventListener(Event.ENTER_FRAME, animateOnCanvas);
}
function animateOnStage(event:Event) {
// move the pieces
for (var i:int = pieces.length - 1; i > -1; i--){
var obj = pieces[i];
obj.bitmap.x += obj.xSpeed;
obj.ySpeed += .5;//gravity
obj.bitmap.y += obj.ySpeed;
if (obj.bitmap.y > yMax){
pieces.splice(i,1);
}
}
// stop animation when last piece drops off screen
if (!pieces.length){
trace("DONE!");
removeEventListener(Event.ENTER_FRAME, animateOnStage);
}
}
function animateOnCanvas(event:Event) {
//clear the bitmap
canvas.bitmapData.fillRect(new Rectangle(0, 0, canvasWidth, canvasHeight), 0);
// move the pieces
for (var i:int = pieces.length - 1; i > -1; i--){
var obj = pieces[i];
obj.x += obj.xSpeed;
obj.ySpeed += .5;//gravity
obj.y += obj.ySpeed;
canvas.bitmapData.copyPixels(obj.bitmapData, new Rectangle(0, 0, size, size), new Point(obj.x, obj.y));
if (obj.y > yMax){
pieces.splice(i,1);
}
}
// stop animation when last piece drops off screen
if (!pieces.length){
trace("DONE!");
removeEventListener(Event.ENTER_FRAME, animateOnCanvas);
}
}
stop();