- You shouldn't use a closure here
game.NPCEnity
does not reference the instance, but the constructor function. If you setthis.settings = …
in the constructor function, you are creating a property on the instance.
You can access the instance with the this
keyword inside your update
function where you handle the collision, where it does point to the player
.
To cite the tutorial:
// call by the engine when colliding with another object // obj parameter corresponds to the other object (typically the player) touching this one onCollision: function(res, obj) {…}
That means you can access the enemies settings by obj.settings
inside that event handler.
If you're not using that handler but calling the collide
method, from what I see the return value of that function has a .obj
property which is presumably the collision target:
if (collision) {
if (collision.obj.type == me.game.ACTION_OBJECT) {
console.log("NPCname: " + collision.obj.settings.name); //get settings from NPCEntity
}
}