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

227
Visualizações
How can I detect when a component leaves the canvas?

I am trying to implement a firing mechanism as in the original.

I don't know how to find out if there's only one projectile existing and when it leaves the upper screen so the player can shoot again.

if (gameArea.key === "Space") {
    playerProjectile = new component(2, 7, "white", player.x + player.width /2, player.y);
    playerProjectile.speedY = +2;

I initialize a new component when the player presses the spacebar. It needs to check if there's still a projectile on the screen, and if it has left the screen.

Here's my component function:

function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;

this.update = function () {
    ctx = gameArea.context;
    ctx.fillStyle = color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
}

this.playerNewPos = function () {
    this.x += this.speedX;
    if (this.x < 0) {
        player.x = 0;
    } else if (this.x > gameArea.canvas.width - player.width) {
        player.x = gameArea.canvas.width - player.width;
    }
}

this.playerProjectileNewPos = function () {
    this.y -= this.speedY;
}
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