拖放Javascript拼图

时间:2017-11-15 05:29:52

标签: javascript

我正在尝试恢复一些旧的代码,这是一个拼图游戏。它从文件夹中加载图像(拼图),随机将它们放在页面上,然后拖放到板上。这曾经有用,但是当我今天尝试使用它时,它只会引发错误(见下文)。

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;
}

我主要得到的错误是:未捕获的TypeError:无法读取未定义的属性'clientX'     在initDrag(easy.html:70)     在HTMLBodyElement.onmousemove(easy.html:116)

我有JSFiddle here。任何人都可以对这个问题有所了解吗?

1 个答案:

答案 0 :(得分:1)

在您的代码中,您有onmousemove="initDrag();"因此,如果document.all是假的,那么mouseX = e.clientX - 50会抛出Cannot read property 'clientX' of undefined,因为您在没有任何关于鼠标移动的争论的情况下调用initDrag()

现在你不应该使用document.all也不要window.event,两者都是残骸。