Código :
import flash.display.*;
import flash.geom.*;
import flash.filters.*;
cimg = 0;
c = 4;
f = 4;
tipo = false;
imagen = "imagen";
var bmp:BitmapData;
var loader:MovieClipLoader = new MovieClipLoader();
loader.addListener(this);
function onLoadInit(target:MovieClip) {
bmp = new BitmapData(target._width, target._height, true, null);
bmp.draw(target);
// aquí puedes hacer lo que quieras ya con tu instancia de bitmapData, ya lo tienes como
// si lo hubieras echo un bmp.loadBitmap();
// o como yo en este caso ponerle un filtro.
//bmp.applyFilter(bmp, bmp.rectangle, new Point(0, 0), new BevelFilter());
target.removeMovieClip();
//this.attachBitmap(bmp, 1);
puzzle(bmp, 4, 4, false);
}
var mc:MovieClip = this.createEmptyMovieClip("tmp_mc", 1);
loader.loadClip("http://www.cristalab.com/images/header/logo.png", mc);
function puzzle(imagen, f, c, lin) {
for (hg=0; hg<4*tpz; hg++) {
pz[hg].removeMovieClip();
}
a = new Array(1, 12, 0, .35, 2, 12, 1, .15, 3, 2, 4, .2, 5, 4, 6, .5, 7, 6, 8, .5, 10, 12, 2, .15, 9, 8, 10, .4, 11, 12, 10, .15);
tpz = f*c;
c>f ? j=20/c : j=20/f;
bF = new BevelFilter(1, 45, 0xffffff, .5, 0, .5, j, j, 4, 3, "inner", false);
img = imagen;
//BitmapData.loadBitmap(imagen);
w = img.width;
h = img.height;
m1 = w/(3*c);
m2 = h/(3*f);
if (!cimg) {
b = [];
}
//solo en demo.
texto = "completo";
// solo en demo.
// creando matriz delados congruentes entre piezas contiguas
p = [];
d = 0;
for (m=0; m<4*tpz; m += 4) {
p[m+7] = p[m+1]=d++;
if ((d-1)%(2*c) == 2*(c-1)) {
p[m+1] = p[(m+4)%(4*tpz)+3]=2*tpz-2;
}
p[m+4*c] = p[m+2]=d++;
if (d>2*(tpz-c) && d%2 == 0) {
p[m+2] = p[(m+4*c)%(4*tpz)]=2*tpz-1;
}
}
for (g=0; g<2*tpz; g++) {
if (!cimg) {
//solo en demo;
b[g] = [];
b[g][0] = new Point(m1, m2);
alea()<0 ? sig=-1 : sig=1;
if (g%2 == 0) {
//puntos para lados horizontales
b[g][12] = new Point(m1, 4*m2);
b[g][4] = new Point(m1-m1*sig*.6, m2*(2+alea()));
b[g][6] = new Point(m1-m1*sig*.9, m2*(2.5+alea()));
b[g][8] = new Point(m1-m1*sig*.6, m2*(3+alea()));
} else {
//puntos para lados verticales
b[g][12] = new Point(4*m1, m2);
b[g][4] = new Point(m1*(2+alea()), m2-m2*sig*.6);
b[g][6] = new Point(m1*(2.5+alea()), m2-m2*sig*.9);
b[g][8] = new Point(m1*(3+alea()), m2-m2*sig*.6);
}
for (r=0; r<29; r += 4) {
//puntos centrales para H y V
b[g][a[r]] = Point.interpolate(b[g][a[r+1]], b[g][a[r+2]], a[r+3]+alea());
}
}
}
//fin cimg solo en demo;
pz = [];
pt = [];
for (n=0; n<f*c; n++) {
pz[n] = createEmptyMovieClip("pz"+n, n+1);
tmp2 = new BitmapData(5*m1, 5*m2, true, 0);
pt[n] = new Point((n%c)*3*m1-m1, Math.floor(n/c)*3*m2-m2);
tmp2.copyPixels(img, new Rectangle(pt[n].x, pt[n].y, 5*m1, 5*m2), null);
//dibujando los 4 lados de cada pieza
for (z=0; z<4; z++) {
z == 2 ? sy=3*m2 : sy=0;
z == 1 ? sx=3*m1 : sx=0;
g = p[4*n+z];
pz[n].beginBitmapFill(tmp2);
pz[n].moveTo(5*m1/2, 5*m2/2);
pz[n].lineTo(b[g][0].x+sx, b[g][0].y+sy);
if (g>(2*tpz-3) || lin) {
pz[n].lineTo(b[g][12].x+sx, b[g][12].y+sy);
} else {
pz[n].lineTo(b[g][1].x+sx, b[g][1].y+sy);
for (j=2; j<12; j += 2) {
pz[n].curveTo(b[g][j].x+sx, b[g][j].y+sy, b[g][j+1].x+sx, b[g][j+1].y+sy);
}
pz[n].lineTo(b[g][12].x+sx, b[g][12].y+sy);
}
pz[n].lineTo(5*m1/2, 5*m2/2);
pz[n].endFill();
}
//doblando la asignacion para evitar fallos por decimales.
pz[n]._x = pt[n].x;
pt[n].x = pz[n]._x;
pz[n]._y = pt[n].y;
pt[n].y = pz[n]._y;
pz[n].filters = [bF];
pz[n].v = n;
}
for (h=0; h<tpz; h++) {
pz[h].onPress = function() {
this.startDrag();
this.swapDepths(tpz+1);
};
pz[h].onRelease = function() {
this.stopDrag();
// iman de colocación si está cerca la pieza.
cerca = new Point(this._x, this._y);
if (Point.distance(pt[this.v], cerca)<4+(m1+m2)/15) {
this._x = pt[this.v].x;
this._y = pt[this.v].y;
}
//comprobador de puzzle completo:
cnt = 0;
for (tt=0; tt<tpz; tt++) {
if ((pz[tt]._y == pt[tt].y) && (pz[tt]._x == pt[tt].x)) {
++cnt;
}
cnt == tpz ? texto="completo" : texto="faltan: "+(tpz-cnt);
}
};
}
cimg = 0;
//solo en demo
}
//fin puzzle
function alea() {
return (.1-Math.random()/5);
}
filamas.onPress = function() {
f++;
puzzle(imagen, f, c, tipo);
};
filamenos.onPress = function() {
f>1 ? f-- : f=1;
puzzle(imagen, f, c, tipo);
};
columas.onPress = function() {
c++;
puzzle(imagen, f, c, tipo);
};
columenos.onPress = function() {
c>1 ? c-- : c=1;
puzzle(imagen, f, c, tipo);
};
mtipo.onPress = function() {
tipo = !tipo;
tipo == false ? txtipo="puzzle" : txtipo="cuadros";
cimg = 1;
puzzle(imagen, f, c, tipo);
};
cambio.onPress = function() {
puzzle(imagen, f, c, tipo);
};
nimagen.onPress = function() {
imagen == "imagen" ? imagen="imagen1" : imagen="imagen";
cimg = 1;
puzzle(imagen, f, c, tipo);
};