灵魂捕手☜ 酔134
关注数: 2 粉丝数: 1 发帖数: 5 关注贴吧数: 5
package {import flash.display package {import flash.display.*;import flash.events.*;import flash.net.URLRequest;import flash.geom.*;import flash.utils.Timer;public class JigsawPuzzle extends MovieClip {// number of piecesconst numPiecesHoriz:int = 3;const numPiecesVert:int = 3;// size of piecesvar pieceWidth:Number;var pieceHeight:Number;// game piecesvar puzzleObjects:Array;// two levels of spritesvar selectedPieces:Sprite;var otherPieces:Sprite;// pieces being dragged var beingDragged:Array = new Array();// load picture and set up spritespublic function startJigsawPuzzle() {// load the bitmaploadBitmap("jigsawimage.jpg");// set up two spritesotherPieces = new Sprite();selectedPieces = new Sprite();addChild(otherPieces);addChild(selectedPieces); // selected on top }// get the bitmap from an external sourcepublic function loadBitmap(bitmapFile:String) {var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);var request:URLRequest = new URLRequest(bitmapFile);loader.load(request);}// bitmap done loading, cut into piecesprivate function loadingDone(event:Event):void {// create new image to hold loaded bitmapvar image:Bitmap = Bitmap(event.target.loader.content);pieceWidth = Math.floor((image.width/numPiecesHoriz)/10)*10;pieceHeight = Math.floor((image.height/numPiecesVert)/10)*10;// place loaded bitmap in imagevar bitmapData:BitmapData = image.bitmapData;// cut into puzzle piecesmakePuzzlePieces(bitmapData);// set up movement and mouse up eventsaddEventListener(Event.ENTER_FRAME,movePieces);stage.addEventListener(MouseEvent.MOUSE_UP,liftMouseUp);} // cut bitmap into piecesprivate function makePuzzlePieces(bitmapData:BitmapData) {puzzleObjects = new Array();for(var x:uint=0;x<numPiecesHoriz;x++) { for (var y:uint=0;y<numPiecesVert;y++) {// create new puzzle piece bitmap and spritevar newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapData,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pieceHeight),new Point(0,0));var newPuzzlePiece:Sprite = new Sprite();newPuzzlePiece.addChild(newPuzzlePieceBitmap);// place in bottom spriteotherPieces.addChild(newPuzzlePiece);// create object to store in array var newPuzzleObject:Object = new Object();newPuzzleObject.loc = new Point(x,y); // location in puzzlenewPuzzleObject.dragOffset = null; // offset from cursornewPuzzleObject.piece = newPuzzlePiece;newPuzzlePiece.addEventListener(MouseEvent.MOUSE_DOWN,clickPuzzlePiece);puzzleObjects.push(newPuzzleObject);} }// randomize locations of piecesshufflePieces();}// random locations for the piecespublic function shufflePieces() { // pick random x and yfor(var i in puzzleObjects) {puzzleObjects[i].piece.x = Math.random()*400+50;puzzleObjects[i].piece.y = Math.random()*250+50;} // lock all pieces to 10x10 gridlockPiecesToGrid();}public function clickPuzzlePiece(event:MouseEvent) {// click locationvar clickLoc:Point = new Point(event.stageX, event.stageY); beingDragged = new Array();// find piece clickedfor(var i in puzzleObjects) {if (puzzleObjects[i].piece == event.currentTarget) { // this is it// add to drag listbeingDragged.push(puzzleObjects[i]);// get offset from cursorpuzzleObjects[i].dragOffset = new Point(clickLoc.x - puzzleObjects[i].piece.x, clickLoc.y - puzzleObjects[i].piece.y);// move from bottom sprite to top oneselectedPieces.addChild(puzzleObjects[i].piece); // find other pieces locked to this onefindLockedPieces(i,clickLoc);break;}}}// move all selected pieces according to mouse locationpublic function movePieces(event:Event) {for (var i in beingDragged) {beingDragged[i].piece.x = mouseX - beingDragged[i].dragOffset.x;beingDragged[i].piece.y = mouseY - beingDragged[i].dragOffset.y; }}// find pieces that should move togetherpublic function findLockedPieces(clickedPiece:uint, clickLoc:Point) {// get list of puzzle objects sorted by distance to the clicked object var sortedObjects:Array = new Array();for (var i in puzzleObjects) {if (i == clickedPiece) continue;sortedObjects.push({dist: Point.distance(puzzleObjects[clickedPiece].loc,puzzleObjects[i].loc), num: i});}sortedObjects.sortOn("dist",Array.DESCENDING);// loop until all linked piece founddo {var oneLinkFound:Boolean = false;// look at each object, starting with closestfor(i=sortedObjects.length-1;i>=0;i--) {var n:uint = sortedObjects[i].num; // actual object number// get the position relative to the clicked objectvar diffX:int = puzzleObjects[n].loc.x - puzzleObjects[clickedPiece].loc.x;var diffY:int = puzzleObjects[n].loc.y - puzzleObjects[clickedPiece].loc.y; // see if this object is appropriately placed to be locked to the clicked oneif (puzzleObjects[n].piece.x == (puzzleObjects[clickedPiece].piece.x + pieceWidth*diffX)) {if (puzzleObjects[n].piece.y == (puzzleObjects[clickedPiece].piece.y + pieceHeight*diffY)) {// see if this object is adj
1 下一页