//not change to the text select form when over text, but rather
//keep its normal arrow appearance. Note that ’this’ inside the
//event-handler definition refers to the tile over which the mouse
//cursor is placed and not to the SlidingPuzzle object instance.
this.tiles[x][y] = $("<div class=’tile’>" +
this.numbers[x][y] + "</div>").appendTo("#" + id);
this.tiles[x][y].click(this.moveTile);
this.tiles[x][y].css("top", x * 1.2 * this.tileSize);
this.tiles[x][y].css("left", y * 1.2 * this.tileSize);
this.tiles[x][y].data("x", x);
this.tiles[x][y].data("y", y);
this.tiles[x][y].data("puzzle", this);
this.tiles[x][y].mouseover(function(){
$(this).css("cursor", "default");
});
}
else {
//If the number represents the empty space, then store its position.
this.empty.x = x;
this.empty.y = y;
}
}
}
//Set the dimensions of the puzzle frame and tiles:
$("#" + id).css("width", 4 * 1.2 * this.tileSize);
$("#" + id).css("height", 4 * 1.2 * this.tileSize);
$(".tile").css("width", this.tileSize);
$(".tile").css("height", this.tileSize);
$(".tile").css("border-radius", 0.2 * this.tileSize);
$(".tile").css("border-width", 0.1 * this.tileSize);
$(".tile").css("fontSize", 0.8 * this.tileSize);
//Attach the keydown event handler to the document. Associate also
//a reference to the puzzle with the document. This is necessary
//because we need to refer to the puzzle from within the
//findAndMoveTile() event-handler function but it is not possible
//to pass arguments to it.
$(document).keydown(this.findAndMoveTile);
$(document).data("puzzle", this);
};
/*
* A keyboard event handler, which is fired on the document. ’this’
* therefore refers to the document object. If a move in the
* desired direction is possible, then it triggers a click event
* on the tile that can move. Since arrow keys have codes from 37
* to 40 (stored in the which property of the Event object), we
* need to subtract 37 before passing the desired move to
* translateMove().
* Parameters:
* event: an Event object holding the additional information
* about the actual event that triggered findAndMoveTile().
*/
B.4. jQu ery 299