I'm trying to make a very basic TypeScript GameEngine but i have a cannot read properties of undefined error has occured and i don't understand why.
This is the error:
Uncaught TypeError: Cannot read properties of undefined (reading 'Update') at u (TSEngine.js:24:14)
My code before converting to javascript:
import Vector2 from "./class/Vector2.js";
import Square from "./class/Square.js";
export default class TSEngine {
indexFileName: string;
s: Square;
v: Vector2;
constructor(indexFileName: string) {
this.indexFileName = indexFileName;
}
Vector2(x: number, y: number): Vector2 {
this.v = new Vector2(x, y);
return this.v;
}
Square(position: Vector2, size: number, color: string): Square {
this.s = new Square(position, size, color);
return this.s;
}
Update(toUpdate: () => void): void {
if(toUpdate) {
toUpdate();
}
}
u(deltaTime: number): void {
this.Update(() => {});
requestAnimationFrame(this.u)
}
}
My code after converting to javascript:
import Vector2 from "./class/Vector2.js";
import Square from "./class/Square.js";
export default class TSEngine {
constructor(indexFileName) {
this.indexFileName = indexFileName;
}
/*getBasePath(): string {
return this.indexFileName;
}*/
Vector2(x, y) {
this.v = new Vector2(x, y);
return this.v;
}
Square(position, size, color) {
this.s = new Square(position, size, color);
return this.s;
}
Update(toUpdate) {
if (toUpdate) {
toUpdate();
}
}
u(deltaTime) {
this.Update(); /// it shows me the error here
requestAnimationFrame(this.u);
}
}
I've tryed to put the 'Update' function above but it doesn't do anything
Edit I'm trying to use this function like that:
Engine.Update(() => {
// the things to update
})
I've put a console.log in the Engine.Update function but it only showed once.