PDA

View Full Version : [AS2] sliding puzzle


nehalacharya
06-22-2009, 06:07 PM
Hi I have been trying to make a sliding puzzle
got this script. it works fine but after the puzzle is solved I would like to move to a next frame with congrats message and also a re-play button can any one help please


the code:

stop();
function slideMe(tile)
{
if (sliding)
{
sliding = false;
slideDirection = "none";
if (tile.posx == emptyX)
{
if (tile.posy == emptyY + 1)
{
slideDirection = "up";
}
else if (tile.posy == emptyY - 1)
{
slideDirection = "down";
} // end else if
}
else if (tile.posy == emptyY)
{
if (tile.posx == emptyX + 1)
{
slideDirection = "left";
}
else if (tile.posx == emptyX - 1)
{
slideDirection = "right";
} // end else if
} // end else if
switch (slideDirection)
{
case "up":
{
tweenTile = new mx.transitions.Tween(tile, "_y", mx.transitions.easing.Regular.easeInOut, tile._y, tile._y - tileHeight, 2.000000E-001, true);
emptyY = emptyY + 1;
tile.posy = tile.posy - 1;
break;
}
case "down":
{
tweenTile = new mx.transitions.Tween(tile, "_y", mx.transitions.easing.Regular.easeInOut, tile._y, tile._y + tileHeight, 2.000000E-001, true);
emptyY = emptyY - 1;
tile.posy = tile.posy + 1;
break;
}
case "left":
{
tweenTile = new mx.transitions.Tween(tile, "_x", mx.transitions.easing.Regular.easeInOut, tile._x, tile._x - tileWidth, 2.000000E-001, true);
emptyX = emptyX + 1;
tile.posx = tile.posx - 1;
break;
}
case "right":
{
tweenTile = new mx.transitions.Tween(tile, "_x", mx.transitions.easing.Regular.easeInOut, tile._x, tile._x + tileWidth, 2.000000E-001, true);
emptyX = emptyX - 1;
tile.posx = tile.posx + 1;
break;
}
case "none":
{
sliding = true;
break;
}
} // End of switch
tweenTile.onMotionFinished = function ()
{
sliding = true;
};

} // end if
} // End of the function
function shuffle(num)
{
for (sh = 1; sh <= num; sh++)
{
directions = new Array("up", "down", "left", "right");
if (emptyX == 1)
{
directions[3] = "left";
}
else if (emptyX == gridWidth)
{
directions[2] = "right";
} // end else if
if (emptyY == 1)
{
directions[1] = "up";
}
else if (emptyY == gridHeight)
{
directions[0] = "down";
} // end else if
randomNumber = Math.floor(Math.random() * 4);
sDirection = directions[randomNumber];
trace ("empty square is: " + emptyX + " " + emptyY);
trace ("direction is: " + sDirection);
switch (sDirection)
{
case "up":
{
tile = getTile(emptyX, emptyY + 1);
tile._y = tile._y - tileHeight;
emptyY = emptyY + 1;
tile.posy = tile.posy - 1;
break;
}
case "down":
{
tile = getTile(emptyX, emptyY - 1);
tile._y = tile._y + tileHeight;
emptyY = emptyY - 1;
tile.posy = tile.posy + 1;
break;
}
case "left":
{
tile = getTile(emptyX + 1, emptyY);
tile._x = tile._x - tileWidth;
emptyX = emptyX + 1;
tile.posx = tile.posx - 1;
break;
}
case "right":
{
tile = getTile(emptyX - 1, emptyY);
tile._x = tile._x + tileWidth;
emptyX = emptyX - 1;
tile.posx = tile.posx + 1;
break;
}
} // End of switch
trace ("tile is: " + tile);
} // end of for
} // End of the function
function getTile(tx, ty)
{
for (i = 1; i <= gridWidth; i++)
{
for (j = 1; j <= gridHeight; j++)
{
if (board["tile" + i + "_" + j].posx == tx && board["tile" + i + "_" + j].posy == ty)
{
return (board["tile" + i + "_" + j]);
} // end if
} // end of for
} // end of for
} // End of the function
Stage.scaleMode = "noScale";
gridWidth = gridHeight = 4;
imageBitmap = flash.display.BitmapData.loadBitmap("india");
tileWidth = imageBitmap.width / gridWidth;
tileHeight = imageBitmap.height / gridHeight;
emptyX = 1;
emptyY = 1;
sliding = true;
this.createEmptyMovieClip("board", 10);
board._x = 400 - gridWidth * tileWidth * 5.000000E-001;
board._y = 300 - gridHeight * tileHeight * 5.000000E-001;
for (i = 1; i <= gridWidth; i++)
{
for (j = 1; j <= gridHeight; j++)
{
if (i != 1 || j != 1)
{
newDepth = i + (j - 1) * gridHeight;
board.createEmptyMovieClip("tile" + i + "_" + j, newDepth);
board["tile" + i + "_" + j]._x = (i - 1) * tileWidth;
board["tile" + i + "_" + j]._y = (j - 1) * tileHeight;
board["tile" + i + "_" + j].posx = i;
board["tile" + i + "_" + j].posy = j;
board["tile" + i + "_" + j].onPress = function ()
{
slideMe(this);
};
this["b" + i + "_" + j] = new flash.display.BitmapData(tileWidth, tileHeight);
tempRect = new flash.geom.Rectangle((i - 1) * tileWidth, (j - 1) * tileHeight, j * tileWidth, j * tileHeight);
this["b" + i + "_" + j].copyPixels(imageBitmap, tempRect, new flash.geom.Point(0, 0));
board["tile" + i + "_" + j].attachBitmap(this["b" + i + "_" + j], 1, true, true);
} // end if
} // end of for
} // end of for
var filter = new flash.filters.DropShadowFilter(10, 45, 0, 8.000000E-001, 16, 16, 1, 2, false, false, false);
var filterArray = new Array();
filterArray.push(filter);
board.filters = filterArray;
startTime = function ()
{
sTime = getTimer();
this.onEnterFrame = function ()
{
tTime = Math.floor((getTimer() - sTime) / 1000);
if (tTime < 10)
{
tTimeDisplay = "00:0" + tTime;
}
else if (tTime < 60)
{
tTimeDisplay = "00:" + tTime;
}
else if (tTime < 600)
{
secs = tTime % 60;
if (secs < 10)
{
secsD = "0" + secs;
}
else
{
secsD = secs;
} // end else if
tTimeDisplay = "0" + Math.floor(tTime / 60) + ":" + secsD;
}
else
{
secs = tTime % 60;
if (secs < 10)
{
secsD = "0" + secs;
}
else
{
secsD = secs;
} // end else if
tTimeDisplay = Math.floor(tTime / 60) + ":" + secsD;
} // end else if
};
};
shuffle(100);
startTime();

nehalacharya
06-23-2009, 08:48 AM
Please do help