1
votes

I make a scene, where I want want to animate cube, but it keeps giving me a error : TypeError: Cannot read property 'rotation' of undefined

        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 works perfectly, but for the cure it gives me an error.

Could anyone help me to solve the problem?

2
is cube a local or global variable? - gaitat
Welcome to Stack Overflow. For whatever reason, cube has the value undefined. Since we can't see where cube has been initialised, you might want to look into what's happening with that. - Qantas 94 Heavy
it should be global. i first declared the variable, then in init function, created a 3d cube, after in render I try to rotate it, and it doesn't work :( - user2839061
Could you please show us your init function? (maybe it's something like a declaration with var or on 'this') you are sure init is ALWAYS called before render? (check Timeouts) - friedrich

2 Answers

0
votes

Have you created cube like this?

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 );

0
votes

Anyway, even getting the same code, that works (really, I went line by line checking them), I changed the line to:

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

                var object = scene.children[ i ];

                if ( object instanceof THREE.Mesh ) {

                    object.rotation.y += 0.1 ;

                    }
            }