Section 1
//BasicButton (BasicButton)
package {
import flash.display.*;
public dynamic class BasicButton extends SimpleButton {
}
}//package
Section 2
//JigsawPuzzle (JigsawPuzzle)
package {
import flash.events.*;
import flash.display.*;
import flash.geom.*;
import flash.net.*;
public class JigsawPuzzle extends MovieClip {
const numPiecesHoriz:int = 10;
const numPiecesVert:int = 10;
public var playAgainButton:BasicButton;
public var startButton:BasicButton;
var pieceWidth:Number;
var pieceHeight:Number;
var puzzleObjects:Array;
var selectedPieces:Sprite;
var otherPieces:Sprite;
var beingDragged:Array;
public function JigsawPuzzle(){
beingDragged = new Array();
super();
addFrameScript(0, frame1, 1, frame2, 2, frame3);
}
public function clickStart(_arg1:MouseEvent){
gotoAndStop("play");
}
public function clickPlayAgain(_arg1:MouseEvent){
gotoAndStop("play");
}
public function startJigsawPuzzle(){
loadBitmap("https://inkbunny.net///files/screen/566/566035_VerbMyNoun_sunny_sourceimage.jpg");
otherPieces = new Sprite();
selectedPieces = new Sprite();
addChild(otherPieces);
addChild(selectedPieces);
}
public function loadBitmap(_arg1:String){
var _local2:Loader = new Loader();
_local2.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var _local3:URLRequest = new URLRequest(_arg1);
_local2.load(_local3);
}
private function loadingDone(_arg1:Event):void{
var _local2:Bitmap = Bitmap(_arg1.target.loader.content);
pieceWidth = (Math.floor(((_local2.width / numPiecesHoriz) / 10)) * 10);
pieceHeight = (Math.floor(((_local2.height / numPiecesVert) / 10)) * 10);
var _local3:BitmapData = _local2.bitmapData;
makePuzzlePieces(_local3);
addEventListener(Event.ENTER_FRAME, movePieces);
stage.addEventListener(MouseEvent.MOUSE_UP, liftMouseUp);
}
private function makePuzzlePieces(_arg1:BitmapData){
var _local3:uint;
var _local4:Bitmap;
var _local5:Sprite;
var _local6:Object;
puzzleObjects = new Array();
var _local2:uint;
while (_local2 < numPiecesHoriz) {
_local3 = 0;
while (_local3 < numPiecesVert) {
_local4 = new Bitmap(new BitmapData(pieceWidth, pieceHeight));
_local4.bitmapData.copyPixels(_arg1, new Rectangle((_local2 * pieceWidth), (_local3 * pieceHeight), pieceWidth, pieceHeight), new Point(0, 0));
_local5 = new Sprite();
_local5.addChild(_local4);
otherPieces.addChild(_local5);
_local6 = new Object();
_local6.loc = new Point(_local2, _local3);
_local6.dragOffset = null;
_local6.piece = _local5;
_local5.addEventListener(MouseEvent.MOUSE_DOWN, clickPuzzlePiece);
puzzleObjects.push(_local6);
_local3++;
};
_local2++;
};
shufflePieces();
}
public function shufflePieces(){
var _local1:*;
for (_local1 in puzzleObjects) {
puzzleObjects[_local1].piece.x = ((Math.random() * 400) + 50);
puzzleObjects[_local1].piece.y = ((Math.random() * 250) + 50);
};
lockPiecesToGrid();
}
public function clickPuzzlePiece(_arg1:MouseEvent){
var _local3:*;
var _local2:Point = new Point(_arg1.stageX, _arg1.stageY);
beingDragged = new Array();
for (_local3 in puzzleObjects) {
if (puzzleObjects[_local3].piece == _arg1.currentTarget){
beingDragged.push(puzzleObjects[_local3]);
puzzleObjects[_local3].dragOffset = new Point((_local2.x - puzzleObjects[_local3].piece.x), (_local2.y - puzzleObjects[_local3].piece.y));
selectedPieces.addChild(puzzleObjects[_local3].piece);
findLockedPieces(_local3, _local2);
break;
};
};
}
public function movePieces(_arg1:Event){
var _local2:*;
for (_local2 in beingDragged) {
beingDragged[_local2].piece.x = (mouseX - beingDragged[_local2].dragOffset.x);
beingDragged[_local2].piece.y = (mouseY - beingDragged[_local2].dragOffset.y);
};
}
public function findLockedPieces(_arg1:uint, _arg2:Point){
var _local4:*;
var _local5:Boolean;
var _local6:uint;
var _local7:int;
var _local8:int;
var _local3:Array = new Array();
for (_local4 in puzzleObjects) {
if (_local4 == _arg1){
} else {
_local3.push({dist:Point.distance(puzzleObjects[_arg1].loc, puzzleObjects[_local4].loc), num:_local4});
};
};
_local3.sortOn("dist", Array.DESCENDING);
do {
_local5 = false;
_local4 = (_local3.length - 1);
while (_local4 >= 0) {
_local6 = _local3[_local4].num;
_local7 = (puzzleObjects[_local6].loc.x - puzzleObjects[_arg1].loc.x);
_local8 = (puzzleObjects[_local6].loc.y - puzzleObjects[_arg1].loc.y);
if (puzzleObjects[_local6].piece.x == (puzzleObjects[_arg1].piece.x + (pieceWidth * _local7))){
if (puzzleObjects[_local6].piece.y == (puzzleObjects[_arg1].piece.y + (pieceHeight * _local8))){
if (isConnected(puzzleObjects[_local6])){
beingDragged.push(puzzleObjects[_local6]);
puzzleObjects[_local6].dragOffset = new Point((_arg2.x - puzzleObjects[_local6].piece.x), (_arg2.y - puzzleObjects[_local6].piece.y));
selectedPieces.addChild(puzzleObjects[_local6].piece);
_local5 = true;
_local3.splice(_local4, 1);
};
};
};
_local4--;
};
} while (_local5);
}
public function isConnected(_arg1:Object):Boolean{
var _local2:*;
var _local3:int;
var _local4:int;
for (_local2 in beingDragged) {
_local3 = Math.abs((_arg1.loc.x - beingDragged[_local2].loc.x));
_local4 = Math.abs((_arg1.loc.y - beingDragged[_local2].loc.y));
if ((((_local3 == 1)) && ((_local4 == 0)))){
return (true);
};
if ((((_local3 == 0)) && ((_local4 == 1)))){
return (true);
};
};
return (false);
}
public function liftMouseUp(_arg1:MouseEvent){
var _local2:*;
lockPiecesToGrid();
for (_local2 in beingDragged) {
otherPieces.addChild(beingDragged[_local2].piece);
};
beingDragged = new Array();
if (puzzleTogether()){
cleanUpJigsaw();
gotoAndStop("gameover");
};
}
public function lockPiecesToGrid(){
var _local1:*;
for (_local1 in puzzleObjects) {
puzzleObjects[_local1].piece.x = (10 * Math.round((puzzleObjects[_local1].piece.x / 10)));
puzzleObjects[_local1].piece.y = (10 * Math.round((puzzleObjects[_local1].piece.y / 10)));
};
}
public function puzzleTogether():Boolean{
var _local2:int;
var _local3:int;
var _local1:uint = 1;
while (_local1 < puzzleObjects.length) {
_local2 = (puzzleObjects[_local1].loc.x - puzzleObjects[0].loc.x);
_local3 = (puzzleObjects[_local1].loc.y - puzzleObjects[0].loc.y);
if (puzzleObjects[_local1].piece.x != (puzzleObjects[0].piece.x + (pieceWidth * _local2))){
return (false);
};
if (puzzleObjects[_local1].piece.y != (puzzleObjects[0].piece.y + (pieceHeight * _local3))){
return (false);
};
_local1++;
};
return (true);
}
public function cleanUpJigsaw(){
removeChild(selectedPieces);
removeChild(otherPieces);
selectedPieces = null;
otherPieces = null;
puzzleObjects = null;
beingDragged = null;
removeEventListener(Event.ENTER_FRAME, movePieces);
stage.removeEventListener(MouseEvent.MOUSE_UP, liftMouseUp);
}
function frame1(){
stop();
startButton.addEventListener(MouseEvent.CLICK, clickStart);
}
function frame2(){
startJigsawPuzzle();
}
function frame3(){
playAgainButton.addEventListener(MouseEvent.CLICK, clickPlayAgain);
}
}
}//package