【问题标题】:Three.js "Uncaught TypeError: Cannot read property 'render' of undefined" error with object literalThree.js“未捕获的TypeError:无法读取未定义的属性'render'”错误与对象文字
【发布时间】:2017-09-09 21:06:19
【问题描述】:

我正在尝试将我的代码转换为对象文字样式。我可以创建场景,但我遇到了动画问题。

在这一行中,我收到“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 的值不会是对您的对象的引用。
  • 那么就不能用对象字面量做动画吗?
  • 是的,但是您必须处理this 如何获得其价值的事实。您可以在createScene() 中声明一个变量并将this 分配给它,然后在事件处理程序中使用该引用。
  • 这不是一个真正的threejs问题。

标签: javascript three.js object-literal


【解决方案1】:

查看我修改后的示例(我借用了您的 objects 函数来渲染立方体……只是为了好玩:)

基本上,您需要使用Function.prototype.bind 将上下文与您的动画方法一起传递

requestAnimationFrame(this.animate.bind(this));

..幕后发生的事情是this.renderer.render(this.scene, this.camera); 的第一次调用发生得很好,没有问题,因为上下文与this.animate(); 方法一起传递。但是,第二次调用animate,通过requestAnimationFrame 方法,上下文不存在。因此,您需要手动传递它。

var three = {
    objects: function() {
        /*objects*/
        var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
				var material = new THREE.MeshBasicMaterial( { color: 0xffaa00 } );
				this.mesh = new THREE.Mesh( geometry, material );
        this.mesh.position.set(24, 14, 12);
				this.scene.add( this.mesh );
    },
    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);
        }.bind(this));

        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);
    },
    animate: function() {
        this.mesh.rotation.x += 0.005;
				this.mesh.rotation.y += 0.01;

        this.renderer.render(this.scene, this.camera);
        requestAnimationFrame(this.animate.bind(this));
    },
    render: function() {
        this.createScene();
        this.animate();
    }
};

three.render();
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.min.js"></script>

<div id="container"></div>

【讨论】:

  • 更改屏幕分辨率时,this.camera.aspect = window.innerWidth / window.innerHeight; 行中出现“Uncaught TypeError: Cannot set property 'aspect' of undefined”错误。它也发生在你的 sn-ps 中。
  • @ozgrozer 是的,因为我猜你调整了窗口大小..我们必须对调整窗口大小的事件处理程序做同样的事情。给我一秒钟..
  • @ozgrozer 我编辑了我的答案。注意.bind(this) 末尾的window.addEventListener
猜你喜欢
  • 2011-07-18
  • 2020-05-16
  • 2015-12-10
  • 2015-06-13
  • 1970-01-01
  • 2012-10-01
  • 2017-05-21
  • 2018-11-05
  • 2016-11-17
相关资源
最近更新 更多