Section 1
//jpg (MidoriPuzzle1.jpg)
package MidoriPuzzle1 {
import flash.display.*;
public dynamic class jpg extends BitmapData {
public function jpg(_arg1:int=600, _arg2:int=800){
super(_arg1, _arg2);
}
}
}//package MidoriPuzzle1
Section 2
//BasicButton (BasicButton)
package {
import flash.display.*;
public dynamic class BasicButton extends SimpleButton {
}
}//package
Section 3
//SlidingPuzzle (SlidingPuzzle)
package {
import flash.events.*;
import flash.display.*;
import flash.geom.*;
import flash.utils.*;
import flash.net.*;
public class SlidingPuzzle extends MovieClip {
public var playAgainButton:BasicButton;
public var startButton:BasicButton;
private var pieceWidth:Number;
private var pieceHeight:Number;
private var puzzleObjects:Array;
private var blankPoint:Point;
private var slidingPiece:Object;
private var slideDirection:Point;
private var slideAnimation:Timer;
static const pieceSpace:Number = 2;
static const horizOffset:Number = 0;
static const vertOffset:Number = 0;
static const numPiecesHoriz:int = 3;
static const numPiecesVert:int = 4;
static const numShuffle:int = 200;
static const slideSteps:int = 10;
static const slideTime:int = 250;
public function SlidingPuzzle(){
addFrameScript(0, frame1, 1, frame2, 2, frame3);
}
public function clickStart(_arg1:MouseEvent){
gotoAndStop("play");
}
public function clickPlayAgain(_arg1:MouseEvent){
gotoAndStop("play");
}
public function startSlidingPuzzle(){
blankPoint = new Point((numPiecesHoriz - 1), (numPiecesVert - 1));
loadBitmap("https://inkbunny.net///files/screen/564/564516_VerbMyNoun_midoripuzzle1.jpg");
}
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);
}
public function loadingDone(_arg1:Event):void{
var _local2:Bitmap = Bitmap(_arg1.target.loader.content);
pieceWidth = (_local2.width / numPiecesHoriz);
pieceHeight = (_local2.height / numPiecesVert);
makePuzzlePieces(_local2.bitmapData);
shufflePuzzlePieces();
}
public 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) {
if (blankPoint.equals(new Point(_local2, _local3))){
} else {
_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);
addChild(_local5);
_local5.x = ((_local2 * (pieceWidth + pieceSpace)) + horizOffset);
_local5.y = ((_local3 * (pieceHeight + pieceSpace)) + vertOffset);
_local6 = new Object();
_local6.currentLoc = new Point(_local2, _local3);
_local6.homeLoc = new Point(_local2, _local3);
_local6.piece = _local5;
_local5.addEventListener(MouseEvent.CLICK, clickPuzzlePiece);
puzzleObjects.push(_local6);
};
_local3++;
};
_local2++;
};
}
public function shufflePuzzlePieces(){
var _local1:int;
while (_local1 < numShuffle) {
shuffleRandom();
_local1++;
};
}
public function shuffleRandom(){
var _local1:Array = new Array();
var _local2:uint;
while (_local2 < puzzleObjects.length) {
if (validMove(puzzleObjects[_local2]) != "none"){
_local1.push(puzzleObjects[_local2]);
};
_local2++;
};
var _local3:uint = Math.floor((Math.random() * _local1.length));
movePiece(_local1[_local3], false);
}
public function validMove(_arg1:Object):String{
if ((((_arg1.currentLoc.x == blankPoint.x)) && ((_arg1.currentLoc.y == (blankPoint.y + 1))))){
return ("up");
};
if ((((_arg1.currentLoc.x == blankPoint.x)) && ((_arg1.currentLoc.y == (blankPoint.y - 1))))){
return ("down");
};
if ((((_arg1.currentLoc.y == blankPoint.y)) && ((_arg1.currentLoc.x == (blankPoint.x + 1))))){
return ("left");
};
if ((((_arg1.currentLoc.y == blankPoint.y)) && ((_arg1.currentLoc.x == (blankPoint.x - 1))))){
return ("right");
};
return ("none");
}
public function clickPuzzlePiece(_arg1:MouseEvent){
var _local2:int;
while (_local2 < puzzleObjects.length) {
if (puzzleObjects[_local2].piece == _arg1.currentTarget){
movePiece(puzzleObjects[_local2], true);
break;
};
_local2++;
};
}
public function movePiece(_arg1:Object, _arg2:Boolean){
switch (validMove(_arg1)){
case "up":
movePieceInDirection(_arg1, 0, -1, _arg2);
break;
case "down":
movePieceInDirection(_arg1, 0, 1, _arg2);
break;
case "left":
movePieceInDirection(_arg1, -1, 0, _arg2);
break;
case "right":
movePieceInDirection(_arg1, 1, 0, _arg2);
break;
};
}
public function movePieceInDirection(_arg1:Object, _arg2, _arg3:int, _arg4:Boolean){
_arg1.currentLoc.x = (_arg1.currentLoc.x + _arg2);
_arg1.currentLoc.y = (_arg1.currentLoc.y + _arg3);
blankPoint.x = (blankPoint.x - _arg2);
blankPoint.y = (blankPoint.y - _arg3);
if (_arg4){
startSlide(_arg1, (_arg2 * (pieceWidth + pieceSpace)), (_arg3 * (pieceHeight + pieceSpace)));
} else {
_arg1.piece.x = ((_arg1.currentLoc.x * (pieceWidth + pieceSpace)) + horizOffset);
_arg1.piece.y = ((_arg1.currentLoc.y * (pieceHeight + pieceSpace)) + vertOffset);
};
}
public function startSlide(_arg1:Object, _arg2, _arg3:Number){
if (slideAnimation != null){
slideDone(null);
};
slidingPiece = _arg1;
slideDirection = new Point(_arg2, _arg3);
slideAnimation = new Timer((slideTime / slideSteps), slideSteps);
slideAnimation.addEventListener(TimerEvent.TIMER, slidePiece);
slideAnimation.addEventListener(TimerEvent.TIMER_COMPLETE, slideDone);
slideAnimation.start();
}
public function slidePiece(_arg1:Event){
slidingPiece.piece.x = (slidingPiece.piece.x + (slideDirection.x / slideSteps));
slidingPiece.piece.y = (slidingPiece.piece.y + (slideDirection.y / slideSteps));
}
public function slideDone(_arg1:Event){
slidingPiece.piece.x = ((slidingPiece.currentLoc.x * (pieceWidth + pieceSpace)) + horizOffset);
slidingPiece.piece.y = ((slidingPiece.currentLoc.y * (pieceHeight + pieceSpace)) + vertOffset);
slideAnimation.stop();
slideAnimation = null;
if (puzzleComplete()){
clearPuzzle();
gotoAndStop("gameover");
};
}
public function puzzleComplete():Boolean{
var _local1:int;
while (_local1 < puzzleObjects.length) {
if (!puzzleObjects[_local1].currentLoc.equals(puzzleObjects[_local1].homeLoc)){
return (false);
};
_local1++;
};
return (true);
}
public function clearPuzzle(){
var _local1:*;
for (_local1 in puzzleObjects) {
puzzleObjects[_local1].piece.removeEventListener(MouseEvent.CLICK, clickPuzzlePiece);
removeChild(puzzleObjects[_local1].piece);
};
puzzleObjects = null;
}
function frame1(){
stop();
startButton.addEventListener(MouseEvent.CLICK, clickStart);
}
function frame2(){
startSlidingPuzzle();
}
function frame3(){
playAgainButton.addEventListener(MouseEvent.CLICK, clickPlayAgain);
}
}
}//package