Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
How to fix Uncaught TypeError: this.newTarget is not a function

When I call newTarget from the tileClick function in class Game I get the error "Uncaught TypeError: this.newTarget is not a function", but when I run it from the function Start I don't get an error and I really don't understand why

Here's my javascript code:

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();
        }
    }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda