Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!