Cuando llamo a newTarget desde la función tileClick en la clase Game, aparece el error "Uncaught TypeError: this.newTarget is not a function", pero cuando lo ejecuto desde la función Start, no recibo ningún error y realmente no lo hago. entender porqué
Aquí está mi código javascript:
function Start() { tiles = document.querySelectorAll(".tile"); var game = new Game(600, 15000, tiles, "playername"); tiles.forEach((tile) => { tile.addEventListener("click", game.tileClick); tile.isTarget = false; tile.style.background = "grey"; }); game.newTarget(); } class Game { constructor(interval, maxTime, tiles, player) { this.points = 0; this.player = player; this.maxTime = maxTime; this.interval = interval; this.tiles = tiles; this.startTime = Date.now(); document.getElementById("points").textContent = this.points; } newTarget() { if (Date.now() - this.startTime > this.maxTime) { clearInterval(this.timer); alert("Game over, you got: " + this.points + " points!"); return; } tiles.forEach((tile) => { tile.isTarget = false; tile.style.background = "grey"; }); var target = Math.floor(Math.random() * tiles.length); tiles.item(target).isTarget = true; tiles.item(target).style.background = "red"; this.timer = setInterval(this.newTarget, this.interval); } tileClick(event) { if (Date.now() - this.startTime > this.maxTime) { return; } this.tile = event.target; if (this.tile.isTarget) { console.log(this.points); this.points++; document.getElementById("points").textContent = this.points; clearInterval(this.timer); this.newTarget(); } } }