here's the code I am using currently
public function resize(event:Event):void {
var image:Loader = event.currentTarget.loader as Loader;
var scale:Number;
if(image.width > image.height) {
scale = stage.stageHeight / image.width;
image.scaleY = scale;
image.scaleX = scale;
image.x = stage.stageWidth;
image.rotation = -90;
addChild(image);
}
else {
scale = stage.stageHeight / image.width;
image.scaleY = scale;
image.scaleX = scale;
addChild(image);
}
// set number of puzzle pieces
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
// cut bitmap into pieces
public function makePuzzlePieces(bitmapData:BitmapData) {
puzzleObjects = new Array();
for(var x:uint=0;x<numPiecesHoriz;x++) {
for (var y:uint=0;y<numPiecesVert;y++) {
// skip blank spot
if (blankPoint.equals(new Point(x,y))) continue;
// create new puzzle piece bitmap and sprite
var newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));
newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapD ata,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pi eceHeight),new Point(0,0));
var newPuzzlePiece:Sprite = new Sprite();
newPuzzlePiece.addChild(newPuzzlePieceBitmap);
addChild(newPuzzlePiece);
// set location
newPuzzlePiece.x = x*(pieceWidth+pieceSpace) + horizOffset;
newPuzzlePiece.y = y*(pieceHeight+pieceSpace) + vertOffset;
// create object to store in array
var newPuzzleObject:Object = new Object();
newPuzzleObject.currentLoc = new Point(x,y);
newPuzzleObject.homeLoc = new Point(x,y);
newPuzzleObject.piece = newPuzzlePiece;
newPuzzlePiece.addEventListener(MouseEvent.CLICK,c lickPuzzlePiece);
puzzleObjects.push(newPuzzleObject);
}
}
}


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks