Преглед изворни кода

Player: Binding object to compiled script so we can access object via this.

Mr.doob пре 11 година
родитељ
комит
ce78ddf689
2 измењених фајлова са 16 додато и 4 уклоњено
  1. 3 0
      editor/js/Menubar.Play.js
  2. 13 4
      editor/js/Player.js

+ 3 - 0
editor/js/Menubar.Play.js

@@ -13,6 +13,9 @@ Menubar.Play = function ( editor ) {
 		player.play();
 
 		var popup = window.open( '', 'preview', 'width=800,height=600' );
+		popup.addEventListener( 'beforeunload', function () {
+			player.stop();
+		} );
 		popup.document.body.style.margin = 0;
 		popup.document.body.appendChild( player.dom );
 

+ 13 - 4
editor/js/Player.js

@@ -16,7 +16,7 @@ var Player = function ( json ) {
 
 		if ( child.script !== undefined ) {
 
-			child.script.compiled = new Function( 'object', 'scene', child.script.source );
+			child.script.compiled = new Function( 'scene', child.script.source ).bind( child );
 
 			scriptObjects.push( child );
 
@@ -35,19 +35,27 @@ var Player = function ( json ) {
 
 	};
 
+	var request;
+
 	var play = function () {
 
-		requestAnimationFrame( play );
+		request = requestAnimationFrame( play );
 		update();
 
 	};
 
+	var stop = function () {
+
+		cancelAnimationFrame( request );
+
+	};
+
 	var update = function () {
 
 		for ( var i = 0; i < scriptObjects.length; i ++ ) {
 
 			var object = scriptObjects[ i ];
-			object.script.compiled( object, scene );
+			object.script.compiled( scene );
 
 		}
 
@@ -58,7 +66,8 @@ var Player = function ( json ) {
 	return {
 		dom: renderer.domElement,
 		setSize: setSize,
-		play: play
+		play: play,
+		stop: stop
 	}
 
 };