Search code examples
if-statementconditional-statementshaxe

Using existence of an object in an if statement in Haxe


Can you use an object's existence as the condition of an if statement in Haxe? If so, how?

var b : Bullet = collide("bullet", x, y);
if (b) {
  b.destroy();
}

I've also tried testing it against the Null type, but that doesn't seem to work.


Solution

  • As kirilloid mentions in the comments, try checking if b is not null:

    var b : Bullet = collide("bullet", x, y);
    if (b != null) {
      b.destroy();
    }
    

    It was decided for Haxe not to have the if(b) syntax for a number of reasons. You can find a discussion on the topic on Google Groups: Test if exists.