I’m trying to integrate a simple slider puzzle game into a flash site.
The original code works fine as one frame on the root time line.
My frustration (sleep deprevation isn’t helping) is that I lose the working integrity of the script when loading the code in a sub movie. The pieces initialize fine. But nothing moves when I click.
So I’m sure this is a movie targeting issue but I’m just missing it.
Hopefully another set of eye ballz can quickly spot what I don’t see.
There are just two blocks of code.
One on the first frame of the movie. :
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 = 3;
numCols = 3;
numCells = numRows*numCols;
xBase = p0._x;
yBase = p0._y;
xSpace = p0._width * 1.025;
ySpace = p0._height * 1.025;
// Create pieces
var c = 1;
var r = 0;
for (var i=1; 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;
}
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) {
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*[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++;
}
}
}
And the other assigned to each button (sliding puzzle piece.)
onClipEvent (mouseDown) {
if (hitTest(_root._xmouse, _root._ymouse, false)) {
var MovNum = Number(substring(this._name, 2, 1))+1;
_root.Click(MovNum);
}
}
I’ve named the instance of the sub movie containing this code and buttons “game.” I thought the following change to the code would work… but no.
onClipEvent (mouseDown) {
if (hitTest(_root.game._xmouse, _root.game._ymouse, false)) {
var MovNum = Number(substring(this._name, 2, 1))+1;
_root.game.Click(MovNum);
}
}
thanks for the help