Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by LaserBeam

Hi!

In the check function I am trying to find out which entity it is, for example:

check: function(other){
      other.receiveDamage(1);
      if(other == EntityDog){
             ...
      }
      else if(other == EntityMan){
             ...
      }     
}

I don't get any errors however it doesn't work either. Any idea?

1 decade ago by Arantor

That's the thing, other won't be identical to the prototype of those things (because things like position and so on will be different)

What are the dog and man representative of? What entity types are in use? Do they have any other properties?

If nothing else, you could define a variable 'thisIs' or similar inside EntityDog and EntityMan, such that other.thisIs will be set to either 'dog' or 'man', when other is referenced. But often, the entity types are suitable enough for this (type A vs type B etc)

1 decade ago by PanTom

If there is only one EntityMan and one EntityDog, use a statement like

if (other instanceof EntityDog) {
}

If there are more than one of a kind, give them a unique name when spawning them...

ig.game.spawnEntity(EntityDog, 10, 10, { name: 'dog' + this.dogCount.toString() });

and check for the entity's name.

1 decade ago by Arantor

I thought instanceOf didn't necessarily work properly due to inheritance in JS?

(But really, the unique name is no different to what I suggested, hehe)

1 decade ago by dominic

instanceof works just fine with Impact's classes. It's one the reasons I choose to base the class system on John Resig's code.

1 decade ago by LaserBeam

'instanceof' works. Thanks guys!

1 decade ago by Arantor

I was going on the comments made in http://impactjs.com/forums/help/finding-an-impact-objects-name-or-type which made me think it didn't work properly...
Page 1 of 1
« first « previous next › last »