Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda