2

这段代码在子级中使用继承我在场景中添加了一些在父级中声明的内容我该怎么做它在子级查看场景中出现错误

 function parent    (domElement, renderStatistics) {
    this.scene = new THREE.Scene();
    }
function child(domElement) {
    parent.call(this, domElement);
    this.init();
}
child.prototype = Object.create(parent.prototype);

child.prototype.constructor = Young;


child.prototype.init = function () {
function createLab(geometry) {
        var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
        this.scene.add(mesh);  // this error Cannot call method 'add' of undefined
    }
}
4

3 回答 3

2
child.prototype.init = function () {
var _this = this;
    function createLab(geometry) {
        var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
        _this.scene.add(mesh);  
    }
}
于 2013-04-08T17:17:04.833 回答
1

看起来你的错误的原因是= =第二行的双等号。

这导致该值的属性是布尔值,而不是您期望的 new THREE.Mesh 的实例。

于 2012-12-10T23:04:56.237 回答
0

我不确定为什么需要在 init 中创建内部函数...

尝试任一

child.prototype.init = function () {
        var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
        this.scene.add(mesh);  // this error Cannot call method 'add' of undefined
}

或者

function createLab(geometry) {
    var mesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
    this.scene.add(mesh);  // this error Cannot call method 'add' of undefined
 };

child.prototype.init = function () {
     createLab.call(this, whatever);
}
于 2012-12-10T22:42:21.537 回答