浏览代码

PointerLockControls: Use EventDispatcher

Mugen87 6 年之前
父节点
当前提交
783679d387
共有 2 个文件被更改,包括 9 次插入9 次删除
  1. 5 5
      examples/js/controls/PointerLockControls.js
  2. 4 4
      examples/misc_controls_pointerlock.html

+ 5 - 5
examples/js/controls/PointerLockControls.js

@@ -10,9 +10,6 @@ THREE.PointerLockControls = function ( camera, domElement ) {
 	this.domElement = domElement || document.body;
 	this.enabled = false;
 
-	this.onLock = null;
-	this.onUnlock = null;
-
 	camera.rotation.set( 0, 0, 0 );
 
 	var pitchObject = new THREE.Object3D();
@@ -42,13 +39,13 @@ THREE.PointerLockControls = function ( camera, domElement ) {
 
 		if ( document.pointerLockElement === scope.domElement ) {
 
-			if ( scope.onLock !== null ) scope.onLock();
+			scope.dispatchEvent( { type: 'lock' } );
 
 			scope.enabled = true;
 
 		} else {
 
-			if ( scope.onUnlock !== null ) scope.onUnlock();
+			scope.dispatchEvent( { type: 'unlock' } );
 
 			scope.enabled = false;
 
@@ -124,3 +121,6 @@ THREE.PointerLockControls = function ( camera, domElement ) {
 	this.connect();
 
 };
+
+THREE.PointerLockControls.prototype = Object.create( THREE.EventDispatcher.prototype );
+THREE.PointerLockControls.prototype.constructor = THREE.PointerLockControls;

+ 4 - 4
examples/misc_controls_pointerlock.html

@@ -118,19 +118,19 @@
 
 				}, false );
 
-				controls.onLock = function() {
+				controls.addEventListener( 'lock', function() {
 
 					instructions.style.display = 'none';
 					blocker.style.display = 'none';
 
-				};
+				} );
 
-				controls.onUnlock = function() {
+				controls.addEventListener( 'unlock', function() {
 
 					blocker.style.display = 'block';
 					instructions.style.display = '';
 
-				};
+				} );
 
 				scene.add( controls.getObject() );