hi,
i'm doing a 4x4 pic shuffle game... using "picture sliding puzzle" source file. but somehow the pieces don't move sometimes. could anyone help me out on this. thank u! thank u!


==script==

if (!initialized) {
Initialize();
initialized = true;
}
function PieceX (col) {
return xBase+col*xSpace;
}
function PieceY (row) {
return yBase+row*ySpace;
}
// Subroutine Initialize
// Puzzle Initialization Code
function Initialize () {
Congratulations.stop();
// Set dimensions of game grid
numRows = 4;
numCols = 4;
numCells = numRows*numCols;
xBase = p0._x;
yBase = p0._y;
xSpace = p0._width;
ySpace = p0._height;
// Create pieces
var c = 0;
var r = 0;
for (var i = 0; i<numCells-1; i++) {
var name = "p"+i;
var newPiece = eval(name);
newPiece._x = PieceX(c);
newPiece._y = PieceY(r);
newPiece.PieceNumber = i+1;
if (++c>=numCols) {
c = 0;
r++;
}
}
// Set initial positions of each cell, including empty space
posArray = [];
for (i=0; i<numCells-1; i++) {
posArray[i] = i;
}
empty = numCells-1;
}
// Subroutine Winner
// Tests whether the current piece positions is a winning position (that is, every piece in sequence)
function isWinner () {
for (var i = 0; i<numCells-1; i++) {
if (posArray[i] != i) {
return false;
}
}
return true;
}
// Subroutine Click
// Puzzle click handler.When a piece is clicked, determine if it is adjacent to the empty space. if it is, move it into the empty space.
// Check if this is a winner
function Click (clicked) {
clicked--;
if (isWinner()) {
// Start a new game
shuffle();
// Get rid of the congratulations message
Congratulations.gotoAndStop(1);
} else {
// Get the position of the clicked piece
pos = posArray[clicked];
// Get row, column of empty spot
emptyRow = Math.floor(empty/numCols);
emptyCol = empty-emptyRow*numCols;
// Get row, column of clicked piece
clickedRow = Math.floor(pos/numCols);
clickedCol = pos-clickedRow*numCols;
// Test adjacency
// adjacent(i, j) = i is above j or i is below j, or i is left of j or i is right of j
rowDiff = Math.abs(clickedRow-emptyRow);
colDiff = Math.abs(clickedCol-emptyCol);
adjacent = (rowDiff+colDiff) == 1;
if (adjacent) {
// Move the movie clip for the piece
var piece = eval("/p"+clicked);
piece._x = PieceX(emptyCol);
piece._y = PieceY(emptyRow);
// Swap the clicked piece with the empty space
posArray[clicked] = empty;
empty = pos;
// If this is a winner, start the winning movie clip
if (isWinner()) {
Congratulations.play();
}
}
}
}
function Shuffle () {
// We want to arrange the cells in a random
// order. We do this by generating a random
// number r(i) for each cell i, and sorting
// the pairs (i, r(i)) using r(i) as the key.
// Comparison function for the sort
var cf = function (x, y) { if (x[1]<y[1]) {return -1;} else if (x[1]>y[1]) {return 1;} else {return 0;}};
var i;
var cell = [];
for (i=0; i<numCells; i++) {
cell.push([i, Math.random()]);
}
cell.sort(cf);
// We've sorted the ordered pairs...
// Now position the pieces according
// to the new order.
var r = 0, c = 0;
for (i=0; i<numCells; i++) {
var piece = cell[i][0];
if (piece == numCells-1) {
empty = i;
} else {
posArray[piece] = i;
var p = eval("/p"+piece);
p._x = PieceX(c);
p._y = PieceY(r);
}
if (++c>=numCols) {
c = 0;
r++;
}
}
}