0

I'm creating a melonJS game.

In my player entity - game.PlayerEntity = me.ObjectEntity.extend({, its update function checks for collision:

game.PlayerEntity = me.ObjectEntity.extend({
   var collision = me.game.collide(this); 

   if (collision) {
       if (collision.obj.type == me.game.ACTION_OBJECT) {
           console.log("NPCname: " + game.NPCEntity.init); //get settings from NPCEntity
       }
   }

Then in my NPC entity object, game.NPCEntity = me.ObjectEntity.extend ({, I want to return settings.name to the PlayerEntity above. So to do this I've created a closure returnSettings().

1) console.log(settings.name) outputs "Lee" as expected

2) return settings.name outputs "undefined"

Why is this?

game.NPCEntity = me.ObjectEntity.extend ({
    init : function(x, y, settings) {
        settings.image = "wheelie_right";
        settings.spritewidth = 64;
        settings.name = "Lee";
        this.parent(x, y, settings);

        this.collidable = true;
        this.type = me.game.ACTION_OBJECT;

        console.log(settings.name); //works
        console.log("returning... " + returnSettings()); //works

        //closure to return settings values externally
        function returnSettings () {
            return settings.name; //returns undefined when called by PlayerEntity above.
        }
    },

Thanks!

Dan
  • 9,391
  • 5
  • 41
  • 73
user3871
  • 12,432
  • 33
  • 128
  • 268
  • 1
    Are you calling the *returnSettings* function? In the above you have `console.log("NPCname: " + game.NPCEntity.returnSettings)`. And there is no need for the function, just do `this.settings = settings` and access the properties directly. – RobG Mar 16 '14 at 20:36
  • I'm trying to access game.NPCEntity's `settings` from game.PlayerEntities object. – user3871 Mar 16 '14 at 20:41
  • Your first example is a syntax error, you're mixing a function with an object literal. – Bergi Mar 16 '14 at 20:44
  • Now that you are at least calling it, the `returnSettings` closure is not a property of the `NPCEntity` object but the return value of the `init` function! – Bergi Mar 16 '14 at 20:50
  • @Bergi yes I see that. I fixed above. still getting `Cannot call method 'returnSettings' of undefined` – user3871 Mar 16 '14 at 20:51
  • I said *return value*, not "property of". Where are you calling `init`? – Bergi Mar 16 '14 at 20:52
  • @Bergi actually I'm not sure where `init` is being called. I've looked through the melonJS API http://melonjs.github.io/docs/me.ObjectEntity.html – user3871 Mar 16 '14 at 21:02
  • You really should go with Robs suggestion and just create a property instead of using a closure. – Bergi Mar 16 '14 at 21:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49849/discussion-between-growler-and-bergi) – user3871 Mar 16 '14 at 21:11

2 Answers2

1
  1. You shouldn't use a closure here
  2. game.NPCEnity does not reference the instance, but the constructor function. If you set this.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
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Apparently you can call the function before its definition because it's defined in function name(){} fashion instead of expresion: https://stackoverflow.com/a/261682/880114

But I don't see where you return that function to call it from elsewhere.

Community
  • 1
  • 1
Nikolay Tsenkov
  • 1,128
  • 10
  • 26