Timer Crap
Arrg med møje og besvær har jeg fået en timer sat på dette script, når man klikker på spillet starter timeren og det er jo godt, men desværrer kan jeg ikke få den stoppet rigtigt, istedet for at stoppe starter den forfra, Det jeg gerne vil have er at den stopper.Her er scriptet.
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] = 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
timerstart();
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()) {
timerstop();
Congratulations.play();
}
}
}
}
function timerstop()
{
_root.startTid = getTimer();
_root.playTid = true;
}
function timerstart()
{
_root.startTid = getTimer();
_root.playTid = true;
}
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++;
}
}
}
