import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Rectangle; import flash.geom.Point; import flash.events.MouseEvent; import flash.display.MovieClip; import flash.events.KeyboardEvent; //hide the successText text field successText.visible = false; var trafficBitmapData:traffic = new traffic(); var destTraffic:BitmapData; var piece:Object = {width:trafficBitmapData.width / 4, height:trafficBitmapData.height / 3}; var puzzle:Object = {cols:4, rows:3}; //create a variable named pieceName as a String but do not assign it a value var pieceName:String; //create a new array named unJumbledPieces and assign it an empty array var unJumbledPieces:Array = new Array(); //create a new array named jumbledPieces and assign it an empty array var jumbledPieces:Array = new Array(); //create a variable named numberCorrect but do not assign it a variable var numberCorrect; puzzleBoard.addEventListener(MouseEvent.CLICK, movePiece); //add an eventListener to the stage to listen for the KEY_UP event that calls the solvePuzzle function stage.addEventListener(KeyboardEvent.KEY_UP,solvePuzzle); function solvePuzzle(evt:KeyboardEvent):void { //use an if statement to check for the s key keyCode value of 83 if(evt.keyCode == 83) // S or s key { //use a for loop to iterate through all of the children of the puzzleBoard movie clip //and place the child at its targetX and targetY properties //see the createPieces function for the targetX and targetY properties for(var i:int=0;i <= puzzleBoard.numChildren - 1; i++) { trace("puzzleBoard.numChildren=",puzzleBoard.numChildren,"& i=",i); trace("puzzleBoard.getChildAt(i)=",puzzleBoard.getChildAt(i)); var tempSolve = puzzleBoard.getChildAt(i); trace("tempSolve=",tempSolve); tempSolve.x = tempSolve.targetX; tempSolve.y = tempSolve.targetY; } //call the checkForWin function checkForWin(); } } function movePiece(evt:MouseEvent):void { trace("Begin movePiece()"); trace("evt.target=",evt.target); var tempPiece = evt.target; trace("tempPiece.name=",tempPiece.name); //in the if below check to see if the tempPiece.name is not equal to "blankPiece" if(tempPiece.name != "blankPiece") { //create a new variable named tempBlank and use the getChildByName method to assign it one of the puzzle pieces //remember the piece is a child of the puzzleBoard movie clip var tempBlank = puzzleBoard.getChildByName("blankPiece"); trace("tempBlank=",tempBlank,"tempBlank.name=",tempBlank.name); //in the if below check to see if the tempBlank piece is colliding with the tempPiece movie clip (hitTestObject) if(tempPiece.hitTestObject(tempBlank)) { //in the if below check to see if tempBlank.x is equal to tempPiece.x or tempBlank.y is equal to tempPiece.y if(tempBlank.x == tempPiece.x || tempBlank.y == tempPiece.y) { //create a var named targetX and assign it the x property of tempBlank var targetX = tempBlank.x; //create a var named targetY and assign it the y property of tempBlank var targetY = tempBlank.y; //set the x property of the tempBlank movie clip to the x property of tempPiece.x; tempBlank.x = tempPiece.x; //set the y property of the tempBlank movie clip to the y property of tempPiece.y; tempBlank.y = tempPiece.y; //set the x property of tempPiece to the targetX value tempPiece.x = targetX; //set the y property of tempPiece to the targetY value tempPiece.y = targetY; //call the checkForWin function checkForWin(); } } } } function createPieces() { var counter = 0; for(var row = 0; row <= puzzle.rows - 1; row++) { for(var col = 0; col <= puzzle.cols - 1; col++) { var newPiece:MovieClip = new MovieClip(); destTraffic = new BitmapData(piece.width, piece.height); if(row == puzzle.rows - 1 && col == puzzle.cols - 1) { destTraffic = new BitmapData(piece.width, piece.height, false, 0xFFFFFF); pieceName = "blankPiece"; } else { destTraffic.copyPixels(trafficBitmapData, new Rectangle(col * piece.width, row * piece.height, piece.width, piece.height), new Point(0, 0)); pieceName = row + " " + col; } var newBitmap:Bitmap = new Bitmap(destTraffic); newPiece.addChild(newBitmap); newPiece.targetX = col * piece.width; newPiece.targetY = row * piece.height; newPiece.x = jumbledPieces[counter][0][0]; newPiece.y = jumbledPieces[counter][0][1]; counter++; newPiece.name = pieceName; puzzleBoard.addChild(newPiece); } } } function checkForWin() { trace("checkForWin() says hello!"); //set the numberCorrect variable equal to 0 numberCorrect = 0; for(var c = 0; c <= puzzleBoard.numChildren - 1; c++) { //create a variable named tempChild and assign it the child at c using getChldAt //remember the child is a child of the puzzleBoard movie clip var tempChild = puzzleBoard.getChildAt(c); trace("tempChild=",tempChild,"c=",c); trace("tempChild.x=",tempChild.x,"tempChild.y=",tempChild.y); trace("tempChild.targetX=",tempChild.targetX,"tempChild.targetY=",tempChild.targetY); //in the if below check to see if the x and y of the tempChild is equal to the targetX and targetY properties of tempChild //you should be using the && AND operator if(tempChild.x == tempChild.targetX && tempChild.y == tempChild.targetY) { trace("numberCorrect=",numberCorrect); //increment numberCorrect numberCorrect++; } } //in the if below check to see if numberCorrect is equal to 12 if(numberCorrect == 12) { //remove the CLICK event listener from the puzzleBoard movie clip puzzleBoard.removeEventListener(MouseEvent.CLICK, movePiece); //show the successText text field successText.visible = true; } } function buildUnJumbled() { var i = 0; for(var jr = 0; jr <= puzzle.rows - 1; jr++) { for(var jc = 0; jc <= puzzle.cols - 1; jc++) { i++; unJumbledPieces.push(new Array(jc * piece.width, jr * piece.height)); } } } function shufflePieces():void { jumbledPieces = []; var i:int = unJumbledPieces.length; var j:int; while(i){ i--; j = (Math.random() * unJumbledPieces.length); jumbledPieces.push(unJumbledPieces.splice(j, 1)); } unJumbledPieces = []; } buildUnJumbled(); shufflePieces(); createPieces();