1

I have a method hitTest that check for collision detection and can return a Point object (if a collision is happened) or (if there is no collision) it returns null or undefined (i haven't deeply understand when it return null or undefined but i trust chrome console).

I have to test collision on 2 objects. And check if one or the two collisions are happening. I have tried this code:

var result1 = hitTest(player, object1);
var result2 = hitTest(player, object2);
if( result1 || result2 )  { blabla() };

but it doesn't work.

now.. i know that js is reallly a tricky language and i think about a smart way to do this without writing typeof 4 times. I'm thinking about python short-circuit logical operators...

4
  • || returns the first found true, and false if not true found. && returns the first found false, and true if not false found. Commented Feb 19, 2013 at 19:17
  • thanks. add it as answer and i can use it as right answer Commented Feb 19, 2013 at 19:22
  • Do you want your if statement to be true if there are (0, at least 1, 2) collisions? Commented Feb 19, 2013 at 19:32
  • i wanto my if pass if at least 1 is true Commented Feb 19, 2013 at 19:46

3 Answers 3

4

You can use &&, it returns the first detected false/null/undefined/0, i.e. if won't pass, if either result1 or result2 is null.

Sign up to request clarification or add additional context in comments.

2 Comments

A better way to say this is: For && to pass, BOTH have to be true. For || to pass, only one has to be true.
@krillgar Yep, if we were talking about if. I was talking about && operator, which really returns either of its operands. Though fixed the answer, since if itself is not failing...
1

for this type of thing, underscore.js is beautifull: http://underscorejs.org/#isNull and http://underscorejs.org/#isUndefined

I use these helpers frequently to get around edge cases in JS such as the ones you mentioned

Comments

1

You wouldn't need to write typeof 4 times already but anyway;

Coercion paradigm for conditional statements and operators:

//TYPE           //RESULT
Undefined        // false
Null             // false
Boolean          // The result equals the input argument (no conversion).
Number           // The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String           // The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object           // true

From Mozilla:

Logical AND (&&)

expr1 && expr2
If the first operand (expr1) can be converted to false, the && operator returns false rather than the value of expr1.

Logical OR (||)

expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

true || false // returns true
true || true // returns true
false || true // returns true
false || false // returns false
"Cat" || "Dog"     // returns Cat
false || "Cat"     // returns Cat
"Cat" || false     // returns Cat

true && false // returns false
true && true // returns true
false && true // returns false
false && false // returns false
"Cat" && "Dog" // returns Dog
false && "Cat" // returns false
"Cat" && false // returns false

Additionally, you can use a shortcut isset() method just like in PHP to properly validate your objects:

function isSet(value) {
    return typeof(value) !== 'undefined' && value != null;
}

So; your code would be:

var result1 = hitTest(player, object1),
    result2 = hitTest(player, object2);
if ( isSet(result1) && isSet(result2) )  { blabla(); };

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.