/* * IMAGEPUZZLE * * One of those puzzles where you can slide a square into the only empty slot, creating a new empty slot. * * To recreate, you'll need to have the Flash9 ActionScript3 Preview installed, you can get it at http://labs.adobe.com/technologies/flash9as3preview/ * Create a new 300x256 FLA, and put 'ImagePuzzle' (without quotes) as its Document Class. Test Movie et voila! * * @author Edwin Heijmen * @version 0.1 */ package { import flash.display.*; import flash.net.*; import flash.events.*; import flash.geom.*; public class ImagePuzzle extends MovieClip { private var imageW:Number; // width private var imageH:Number; // height private var cols:uint = 5; // number of columns private var rows:uint = 5; // number of rows private var pieceW:Number; // width of single piece private var pieceH:Number; // height of single piece private var emptyCol:uint; // column of empty piece private var emptyRow:uint; // row of empty piece // used for loading image, interactivity and empty piece private var img:Loader; private var image:Bitmap; private var origData:BitmapData; private var bmd:BitmapData; private var bm:Bitmap = null; private var sp:Sprite = new Sprite(); private var whiteBmd:BitmapData; public function ImagePuzzle () { // constructor // // load in picture img = new Loader(); img.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingCompleted); var requestImage:URLRequest = new URLRequest("ggoo_300x256.jpg"); img.load(requestImage); } private function loadingCompleted (event:Event):void { // loading is done, start working // // get width & height imageW = img.contentLoaderInfo.width; imageH = img.contentLoaderInfo.height; // // create mouselistener sp.graphics.beginFill(0xFFFFFF); sp.graphics.lineTo(imageW,0); sp.graphics.lineTo(imageW,imageH); sp.graphics.lineTo(0,imageH); sp.graphics.lineTo(0,0); sp.graphics.endFill(); addChild(sp); sp.addEventListener("mouseUp",checkPuzzle); // // determine size of each rectangle of puzzle pieceW = imageW/cols; pieceH = imageH/rows; // first make sure we can distort image bm = Bitmap(img.content); bmd = bm.bitmapData; addChild(bm); // // set emptyCol&Row emptyCol = cols-1; emptyRow = rows-1; // remove bottom-right piece var rect:Rectangle = new Rectangle(0,0,pieceW,pieceH); whiteBmd = new BitmapData(pieceW,pieceH,false,0xFFFFFF); var p:Point = new Point(emptyCol*pieceW,emptyRow*pieceH); bmd.copyPixels(whiteBmd,rect,p); // // first randomize the puzzle randomizePuzzle(); } private function checkPuzzle (event:Event):void { // distort rectangles inside image based on sound channels // // // get mouse position var mX:uint = mouseX; var mY:uint = mouseY; // determine which column & row mouse is on var mCol:uint = Math.floor(mX/pieceW); var mRow:uint = Math.floor(mY/pieceH); // the only pieces of the puzzle that can move are the ones // that are either 1 column or 1 row away from the empty piece but not both // so if we add the absolute difference between the piece the mouse is on // and the empty piece of columns and rows together, // it can only swap piece if this difference is 1. // if it is 0 the mouse is on the empty piece, // if it is larger than 1, the mouse is not on a piece right next to the emtpy piece if (Math.abs(mCol-emptyCol)+Math.abs(mRow-emptyRow)==1) { // piece the mouse is on and empty piece can be swapped // movePiece(mCol,mRow); } } private function randomizePuzzle ():void { // randomizes the puzzle by moving pieces a number of times // becuz it only performs allowed slides, the puzzle is always solvable // var newCol:uint; var newRow:uint; for (var r:uint=0;r<50;++r) { if (Math.random()<0.5) { // move horizontally // newRow = emptyRow; // if empty piece is not at either edge, chose left or right if (emptyCol>0&&emptyCol0&&emptyRow