Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将加载的模型分配给对象3中的值_Javascript_Three.js - Fatal编程技术网

Javascript 将加载的模型分配给对象3中的值

Javascript 将加载的模型分配给对象3中的值,javascript,three.js,Javascript,Three.js,我对Three.js相当陌生。我想将一个3D模型加载到Three.js中,并将加载的模型作为一个值保存在我的对象中,但当我这样做时,我以后无法在我的对象的其他函数中使用这个.instance constructor(name,chords,scene){ this.instance; var mtlLoader = new THREE.MTLLoader(); mtlLoader.setTexturePath(`/assets/${name

我对Three.js相当陌生。我想将一个3D模型加载到Three.js中,并将加载的模型作为一个值保存在我的对象中,但当我这样做时,我以后无法在我的对象的其他函数中使用这个.instance

    constructor(name,chords,scene){
        this.instance;
        var mtlLoader = new THREE.MTLLoader();
        mtlLoader.setTexturePath(`/assets/${name}/`);
        mtlLoader.setPath(`/assets/${name}/`);
        mtlLoader.load(`${name}.mtl`, (materials) => {
            materials.preload();
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath(`/assets/${name}/`);
            objLoader.load(`${name}.obj`, (object) => {
                this.instance=object;
                
                scene.add(this.instance);
                
            });
    });```

首先:行
this.instance无效。此外,“我以后不能在其他函数中使用this.instance”是什么意思。访问
实例时是否出现运行时错误

如果您想安全起见,请将其作为第一行放在构造函数中:

const scope=this;

然后在您的
onLoad()
回调中使用
scope.instance=object

我添加了您告诉我的内容,但是仍然获取未捕获的TypeError:this.instance是未定义的
错误,当我想访问object中其他函数中的this.instance时,请用一个实例演示运行时错误:我将整个项目粘贴在这里:抱歉,但我们的想法是共享一个演示此问题的实例。您刚刚共享了您的代码(它现在抛出了不相关的运行时错误)。很抱歉,我不明白,我在使用
this.instance.position.set(和弦[0],和弦[1],和弦[2])时得到了
未捕获的TypeError:this.instance是未定义的
;this.instance.rotation.set(和弦[3],和弦[4],和弦[5])