This collision code (referenced verbatim from Mozilla) works:
if (rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.height + rect1.y > rect2.y) {
// collision detect!
//do something, like this...
rect1.x + 100;
}
However, if written as a function, it never rings true (nothing happens).
Function:
function MozillaCollision(ObjectA, ObjectB) {
if (ObjectA.x < ObjectB.x + ObjectB.width && ObjectA.x + ObjectA.width > ObjectB.x && ObjectA.y < ObjectB.y + ObjectB.height && ObjectA.height + ObjectA.y > ObjectB.y) {
// collision detected!
return true;
}
else {
return false;
}
}
Calling the function:
if (MozillaCollision(rect1, rect2))
{
//do something, like this...
rect1.x + 100;
}
Nothing ever happens, despite the code working when it's outside of a function/written inline!
If I change the function call by moving its brackets slightly, like this, then it is constantly being called true (rect1 is constantly moving by 100 pixels on the x axis, despite the fact that rect1 and rect2 aren't actually colliding, so I can only assume that this is not how functions are written!):
if (MozillaCollision)(rect1, rect2)
{
//do something, like this...
rect1.x + 100;
}
The fact that it either executes constantly or not at all depending upon placement of brackets is confusing (I'm learning the syntax). This confusion is exacerbated by the fact that the collision code works just fine when it is not written as a function that is being called.
My question, then, is about the correct JavaScript syntax for writing functions. I'm seemingly writing this wrong (code works otherwise sans being in a function), and any clarification's appreciated.