joshmerd
joshmerd

Reputation: 57

Drag and Drop Javascript Puzzle

I'm trying to revive some old code I have which is a jigsaw puzzle. It loads images (puzzle pieces) from a folder, randomly places them around the page, and then you drag and drop onto the board. This used to work, but when I tried to use it today, it just throws errors (see below).

HTML:

<body onload="init();" onmousemove="initDrag();" onmouseup="mousePress=false;">
  <div id="main">
    <div id="inside">
      <img src="holder.gif" style="position:absolute; left:500; top:500;" />
    </div>
  </div>
  <div style="position:absolute;top:10px;left:600px;display:none;">
  </div>
  <p id="pText" class="congrats" style="position:absolute; top:410px; left:475px;"></p>
  <p style="position:absolute; top:20px; left:750px;" class="links"><a href="hard.html">hard puzzle</a> | <a href="main.html">home</a></p>
</body>

Javascript:

// ARRAY FOR PLACEMENT OF IMAGES: [down, over]
var place = new Array([51,300],[51,432],[51,569],[51,707],[112,300],[112,432],[112,569],[112,707],[199,300],[199,432],[199,569],[199,707],[286,300],[286,432],[286,569],[286,707]);

var displayed = new Array();
var mousePress = false;
var pieces = place.length;
var placed = new Array();

var moveObject;

function init() {
    var append = "";
    for ( i=0; i<pieces; i++ )
    {
        append+= '<div onDrag="return false;" unselectable=on id="puzzle" name="puzzle" class="puzzleMain"><img src="img/us/img' + i + '.gif" onmousedown="mousePress=true;moveObject=' + i + ';if(document.all) {offsetX=window.event.offsetX;offsetY=window.event.offsetY;}" onmouseup="mousePress=false;doplace(' + i + ');"></div>';
        displayed[i] = 0;
        placed[i]=0;
    }
    document.getElementById("main").innerHTML += append;
    document.images[i].onload = rndmImg;
}

function rndmImg() {
    x=855;y=50;
    do {
        do {
            displayNext = Math.floor( Math.random()* pieces );
        } while( displayed[displayNext] );
        document.getElementsByName("puzzle")[displayNext].style.top = y;
        document.getElementsByName("puzzle")[displayNext].style.left = x;
        document.getElementsByName("puzzle")[displayNext].style.visibility="visible";
        displayed[displayNext]=1;
        x += document.images[displayNext].width;
        if( x >= 900 ) {
            y += document.images[displayNext].height;
            if ( y>=300 ) { x=0 } else { x=851 }
        }
    } while(!allDisplayed());
}

function allDisplayed() {
    for( z=0; z<displayed.length; z++ ) if( !displayed[z] ) return false;
    return true;
}

function lower() {
    for( p=0;p<document.getElementsByName("puzzle").length;p++ )
    document.getElementsByName("puzzle")[p].style.zIndex = 1;
    document.getElementsByName("puzzle")[moveObject].style.zIndex=5;
}

function initDrag(e) {
    if(!mousePress)return;
    if( document.getElementById("inside").innerHTML != "" ) document.getElementById("inside").innerHTML = "";
    lower();
    if(document.all) {
        mouseX = window.event.clientX - (offsetX);
        mouseY = window.event.clientY - (offsetY);
    } else {
        mouseX = e.clientX - 50;
        mouseY = e.clientY - 50;
    }
    document.getElementsByName("puzzle")[moveObject].style.top=mouseY;
    document.getElementsByName("puzzle")[moveObject].style.left=mouseX;
    return false;
}

function doplace(index) {
    w = document.images[index].width;
    h = document.images[index].height;
    t = parseInt(document.getElementsByName("puzzle")[index].style.top);
    l = parseInt(document.getElementsByName("puzzle")[index].style.left);

    if ( ( l >= place[index][1]-(w/2) && l <= place[index][1]+(w/2) ) && ( t >= place[index][0]-(h/2) && t <= place[index][0] + (h/2) ) ) 
    {
        document.getElementsByName("puzzle")[index].style.top = place[index][0];
        document.getElementsByName("puzzle")[index].style.left = place[index][1];
        placed[index] = 1;
        if(isComplete())
            pText.innerHTML = "Puzzle is complete!";
    }

}

function generateArray()
{
    append="var place = new Array(";
    for( i=0; i<document.getElementsByName("puzzle").length; i++ ) 
    {
        t = document.getElementsByName("puzzle")[i].style.pixelTop;
        l = document.getElementsByName("puzzle")[i].style.pixelLeft;
        append+="[" + t + "," + l + "],";
    }
    document.forms[0].mArray.value = append;
}

function isComplete()
{
    for( q=0;q<placed.length;q++ ) if( !placed[q] )return false;
    return true;
}

The error I mostly get is: Uncaught TypeError: Cannot read property 'clientX' of undefined at initDrag (easy.html:70) at HTMLBodyElement.onmousemove (easy.html:116)

I have a JSFiddle here. Can anyone shed some light on what the issue might be?

Upvotes: 1

Views: 1207

Answers (1)

t.niese
t.niese

Reputation: 40862

In your code you have onmousemove="initDrag();" so if document.all is falsy then mouseX = e.clientX - 50 would throw a Cannot read property 'clientX' of undefined because you call initDrag() without any argument on mouse move.

Now adays you neither should use document.all nor window.event, both are relicts.

Upvotes: 1

Related Questions