View Full Version : sliding puzzle game

12-12-2010, 01:08 AM
Please can anyone help?

hello I am novice when it comes to writing code and as i'm writing some code for a sliding puzzle game, i have managed to display the images.

but now i need to write the functions to see if the image when clicked on is next to the blank one and then to move them if this is true.

<Title> Sliding Puzzle Game</Title>

<Script Language = "JavaScript">

var images = new Array();
//var initialise
var id = 0;
window.onerror = handleError;

function handleError(strMessage, strURL, intLine)
//the above function catches any errors and display what and where.
document.write("<br/>The following Error occurred:");
document.write("<br/>Message: " + strMessage);
document.write("<br/>Code: " + strURL);
document.write("<br/>On line: " + intLine);

function Write2screen(){

var k=0;

for (i=0; i< 4; i++) {
for (j=0; j<4; j++) {

id++; //give picture unique id after each iteration

images[k] = "Images/" + k + ".jpg";
document.write(" <img src= '"+images[k]+"' id='"+id+"'/>");

function CanMoveTile() {
// checks to see whether the cell at postion cellNum
// is next to the black tile and returns true if and
// only if this is the case


function moveTile(id) {
// this function moves the tile that was clicked on (if the tile can actually be moved)



<body onClick=moveTile(id)>


many thanks in advance

12-12-2010, 01:20 AM
When document.write is called, the document is closed and a new one is written.

Philip M
12-12-2010, 10:55 AM
To expand on mrhoo's comment:-

document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

If you are a novice you have bitten off a very large mouthful. The code is not trivial.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.