Я пытаюсь преобразовать свои коды в литеральный стиль объекта. Я могу создать сцену, но у меня проблемы с анимацией.
В этой строке я получаю сообщение об ошибке «Uncaught TypeError: Cannot read property 'render' of undefined».
this.renderer.render(this.scene, this.camera);
Это мой объект:
var three = {
objects: function() {
/*objects*/
},
createScene: function() {
this.container = document.getElementById("container");
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.001, (26 * 10));
this.camera.position.set(26, (26 / 2), 26);
window.addEventListener("resize", function() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});
this.objects();
this.renderer = new THREE.WebGLRenderer();
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.container.appendChild(this.renderer.domElement);
this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
},
animate: function() {
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(this.animate);
},
render: function() {
this.createScene();
this.animate();
}
};
three.render();
this
в прослушивателе событий не будет ссылкой на ваш объект. - person Pointy   schedule 09.09.2017this
получает свое значение. Вы можете объявить переменную вcreateScene()
и присвоить ейthis
, а затем использовать эту ссылку в обработчике событий. - person Pointy   schedule 09.09.2017