退出pointerlockcontrols three.js无法正常工作

时间:2016-09-06 18:11:34

标签: javascript function three.js pointerlock

我有以下代码来锁定光标(这很好用):

var element = document.body;

var controls;
var instructions = document.getElementById( 'start' );
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;

if ( havePointerLock ) {


    var pointerlockchange = function ( event ) {

        if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {

            controlsEnabled = true;
            controls.enabled = true;

        } else {

            controlsEnabled = false;
            controls.enabled = false;

            instructions.style.display = '';

        }

    };

    var pointerlockerror = function ( event ) {

        instructions.style.display = '';

    };

    // Hook pointer lock state change events
    document.addEventListener( 'pointerlockchange', pointerlockchange, false );
    document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
    document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );

    document.addEventListener( 'pointerlockerror', pointerlockerror, false );
    document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
    document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );

    instructions.addEventListener( 'click', function ( event ) {

        instructions.style.display = 'none';

        // Ask the browser to lock the pointer
        element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;

        if ( /Firefox/i.test( navigator.userAgent ) ) {

            document.addEventListener( 'fullscreenchange', fullscreenchange, false );
            document.addEventListener( 'mozfullscreenchange', fullscreenchange, false );

            element.requestFullscreen = element.requestFullscreen || element.mozRequestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen;

            element.requestFullscreen();

        } else {

            element.requestPointerLock();

        }

    }, false );

} else {

    instructions.innerHTML = 'Your browser doesn\'t seem to support my epic Portfolio';

}

当我运行一个函数时,我想要禁用pointerlockcontrols所以我可以再次使用我的鼠标。所以我尝试了以下内容:

function onDocumentMouseDown( event ) {

    element.exitPointerLock;

}

这不会给我任何错误,但我也不会让我的鼠标回来为什么会这样?

当我尝试:

element.exitPointerLock();

我得到了element.exitPointerLock不是函数

所以我不知道如何解决这个问题:(如果您需要更多代码或jsfiddle,任何建议都会很棒,请问我:)

1 个答案:

答案 0 :(得分:3)

所以我想通了:D我不得不改变:

element.exitPointerLock();

为:

document.exitPointerLock();

然后一切正常!希望这有助于某人:)