I want to pass my scene constant to another class and use it there. But when I try to use it, it gives error of
Uncaught (in promise) TypeError: Cannot read property ‘scene’ of undefined
The one in below is my main file which I do everything:
class Main
{
constructor()
{
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color( 0xaaaaaa );
.
.
.
/* some other code */
}
Init()
{
/* other code */
// Define constructor class and PASS SCENE CONST to it
const cityConstructor = new Constructor(1, this.scene); //<-- THIS IS WHERE I PASS
cityConstructor.ConstructCity();
}
}
const main = new Main();
main.Init();
And this is the class I passed the variable:
export class Constructor{
constructor(cellWidth, scene){
this.scene = scene;//document.getElementsByTagName("canvas")[0].scene; // I tried other things as well
this.loader = new GLTFLoader();
}
LoadAsset(block, xPos, yPos)
{
// load a resource
this.loader.load(
'../assets/'+ block.name,
function ( gltf ) {
const model = gltf.scene;
model.position.set(xPos, 0, yPos);
this.scene.add( model ); // <--- THIS IS WHERE ERROR IS
},
/*Other code*/
);
}
.
.
.
/* Other code */
Now I want to ask what is best practice for accessing THREE.scene object from other classes?