Weird collision error

Link to the project or level: [replace with link to the curriculum or get the “Share” link to a project]
What I expect to happen: Try playing the game (Press the “FIGHT!” button on the bottom). I expect the robots to not be able to go through the triangle.
What actually happens: However, when the robot is on the ground, it is going through the obstacle when it’s at the bottom of the canvas.
What I’ve tried: I tried checking the colliders of the objects it is able to go through. I’ve also noticed in a few other examples that the .collide() isn’t working properly and it only happens when the sprite that is moving is standing on something that is already colliding it. It may have something to do with how collide() works, maybe when the robot is getting collided by the ground, it somehow moves into the triangle. Also, if you try to jump in the triangle, you will be teleported right out of the triangle.

I noticed all of those things happening when I played the game. When the robot is sliding down the triangle the moment it hits the ground it will move into the triangle. Have you tried a nested if, then when you are checking for collisions? Or checking both before the robot moves?

I could better help you if you put descriptive comments in your code. I noticed you have a few comments for yourself, but it helps if we can easily see what each section of code does or what you mean for it to do. It also helps if you include descriptors in the comments like “arena1 is …” then describe what I’m seeing in the game. That way I don’t need to keep going back up to the var commands to figure each line out.

I’ll keep looking at it for a bit and let you know if I have any other ideas. Let me know if you update it with comments and I’ll take another look as well.

1 Like

Hi @IMPixel ,
Since the issue only happens when the object is on the ground, I suspected this was caused by a short circuit of the compound logic statement for isTouchingEdges.

isTouchingEdges2 = isTouchingEdges2 || robot2.collide(currentArena.obstacles) || robot2.collide(currentArena.advancedObstacles);

If the robot is on the bottom edge, then the first part is true from an earlier line and there is no need to evaluate the other parts of the OR statement. When I changed the order to check for the obstacles first, it works the way you expected.

Here is an article on short circuiting.

Hope that helps,


I tried it and it made another weird error:
Did I understand you right? Or did I do something wrong?

Hi @IMPixel ,
It looks like you made two compound logic statements that are both subject to short circuiting which would explain the new weird behavior. In my project, I changed the statements like this:

isTouchingEdges1 = robot1.collide(currentArena.obstacles) || robot1.collide(currentArena.advancedObstacles) || isTouchingEdges1 ;

so it checks for the bottom edge last - the one that is more likely to be true than the others.

Hope that helps, Andrea