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

237
Visualizações
Code is "skipped" when checking for collision

I'm createing a game for a school project, and I need to check when a player collides with a wall, but the function I've written only works for one side of the rectangle

function wallCollision(){
    if (player.x + player.l > wall.x && player.x < wall.x + wall.width){
        if (player.y == wall.y + wall.height){
            player.velocity.negy = 0;
        }
        if (player.y + player.l == wall.y){
            player.velocity.posy = 0;
        }
    }
    if (player.y < wall.y + wall.height && player.y + player.l > wall.y){
        if (player.x == wall.x + wall.width){
            player.velocity.negx = 0;
        }
        if (player.x + player.l == wall.x){
            player.velocity.posy = 0;
        }
    }
}

When I run it only works when checking id the player collided on the left side of the wall

if (player.x == wall.x + wall.width){
    player.velocity.negx = 0;
}

and when I tried to console.log inside the other 3 nested ifs I discovered that the code insade never runs

I've tried riarranging the function but it either didn't work completely or was somehow worse

neg and pos refer to the direction on the x and y axis, so posx = 0 means that the player cannot move right, negx = 0 that it cannot move let, posy = 0 that it cannot go downwards and negy = 0 that it cannot move upwards

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