I'm using babylonjs library and created a "Building" class with typescript. Using typescript for the whole thing BTW. I create this new "Building" from my main game.ts "Game" class and when trying to access a member of "Building" I get "undefined" variable errors. However this only happens within another class method but seems work correctly in the constructor. I'm assuming it has something to do with the "this" scoping in javascript/typescript. I have tried modifying the function by doing:
Create = ...(...)=> {
...
I have tried creating the variable via:
private rect: = () => Rectangle
but this still does not work
Is this really an issue with "this" scoping because nothing seems to be working. Below I marked exactly where this variable works and where this doesnt work.
class Building {
private rect : Rectangle
private buildingMesh:string[]
private buildingId:string
constructor(rect: Rectangle, id:string) {
this.rect = rect
console.log("TL in b const: " + this.rect.topLeft.x) // <--- This works here
this.buildingId = id
}
Create(scene:BABYLON.Scene) {
BABYLON.SceneLoader.ImportMesh(this.buildingId, "models/","tree.babylon", scene, function (newMeshes) {
var idx = 0
console.log("TL in b: " + this.rect.topLeft.x) // <--- this gives me undefined
var wall =newMeshes[0].createInstance(this.buildingId + idx)
wall.position.x = this.rect.topLeft.x
wall.position.y = this.rect.topLeft.y
this.buildingMesh.push(this.buildingId + idx)
idx++
});
}
}