私は babylonjs ライブラリを使用しており、typescript で「Building」クラスを作成しました。ところで、全体にtypescriptを使用しています。メインの game.ts 「ゲーム」クラスからこの新しい「建物」を作成し、「建物」のメンバーにアクセスしようとすると、「未定義」変数エラーが発生します。ただし、これは別のクラス メソッド内でのみ発生しますが、コンストラクターでは正しく機能するようです。javascript/typescriptの「this」スコープと関係があると思います。私は次のようにして関数を変更しようとしました:
Create = ...(...)=> {
...
次の方法で変数を作成しようとしました:
private rect: = () => Rectangle
しかし、これはまだ機能しません
何も機能していないように見えるため、これは本当に「この」スコープの問題ですか。以下に、この変数が機能する場所と機能しない場所を正確にマークしました。
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++
});
}
}