【问题标题】:three.js TypeError: Cannot read property 'rotation' of undefinedthree.js TypeError:无法读取未定义的属性“旋转”
【发布时间】:2015-05-29 17:05:59
【问题描述】:

我制作了一个场景,我想在其中为立方体设置动画,但它一直给我一个错误: TypeError:无法读取未定义的属性“旋转”

        function animate() {
        requestAnimationFrame( animate );
        render();
        }

        function render() {

            var time = Date.now() * 0.005;
            camera.position.x += ( mouseX - camera.position.x ) * 0.05;
            camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
            camera.lookAt( scene.position );

            for ( i = 0; i < scene.children.length; i ++ ) {
                var object = scene.children[ i ];
                if ( object instanceof THREE.PointCloud ) {
                    object.rotation.y += 0.01 ;
                    }
            }
            cube.rotation.y = time* 0.01;
            renderer.render( scene, camera );
        }

PoinCloud 完美运行,但对于治愈它给了我一个错误。

谁能帮我解决这个问题?

【问题讨论】:

  • cube 是局部变量还是全局变量?
  • 欢迎来到Stack Overflow。无论出于何种原因,cube 的值为 undefined。由于我们看不到 cube 的初始化位置,因此您可能想看看发生了什么。
  • 它应该是全局的。我首先声明了变量,然后在 init 函数中创建了一个 3d 立方体,在渲染后我尝试旋转它,但它不起作用:(
  • 你能告诉我们你的初始化函数吗? (也许它类似于带有 var 或在 'this' 上的声明)你确定总是在渲染之前调用 init 吗? (检查超时)

标签: javascript three.js


【解决方案1】:

你创建过这样的立方体吗?

var geometry = new THREE.BoxGeometry( 1, 1, 1 ); var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube );

【讨论】:

  • 是的!我在 init 函数中创建了它。
  • 尝试使用其他浏览器以了解除此之外的任何错误消息并告诉我
  • 仍然没有。如果我将其声明为 var cube = new THREE.Mesh( );它没有显示任何错误,但也没有动画
  • 这可能是问题所在。你在哪里有那行“var cube = THREE.Mesh();”?如果您在函数中有此行,则立方体变量将不在全局空间中。在这种情况下,只需删除 'var'...
【解决方案2】:

不管怎样,即使得到相同的代码,也可以工作(真的,我逐行检查它们),我将行改为:

for ( i = 0; i < scene.children.length; i ++ ) {

                var object = scene.children[ i ];

                if ( object instanceof THREE.Mesh ) {

                    object.rotation.y += 0.1 ;

                    }
            }

【讨论】:

  • 您首先声明对象,然后在渲染函数中添加我在上面发布的代码。
猜你喜欢
  • 1970-01-01
  • 2017-01-07
  • 2013-06-16
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多