尝试在three.js

时间:2018-05-27 08:21:14

标签: javascript animation error-handling three.js undefined

尝试在three.js中旋转对象时,

“对象未定义”错误。代码片段:

var object;

function render() {
    renderer.render(scene, camera);
}

function animate() {
    object.rotation.x += 0.1;
    render();
    requestAnimationFrame(animate);
    controls.update();
}

1 个答案:

答案 0 :(得分:0)

为了简化问题,我只会讨论问题代码:

var object; // undefined
object.rotation.x += 0.1; //trying to access a key inside an undefined

在全局范围内声明object很好,但您只是声明它并且没有将它分配给值。这意味着您正在尝试在undefined的变量中找到一个键。

var object = {};
object.rotation.x += 0.1; // object.rotation is undefined

还不够好。变量object不拥有名为rotation的密钥。这是未定义的。您将尝试键入不存在的密钥。我不确定您的使用案例,但是对于这种情况,将object指定为包含您手动需要的所有密钥的对象将解决您遇到的undefined问题。

var object = {
    rotation: {
        x: 0
    }
};
object.rotation.x += 0.1;