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 fulvio

I am trying to access the following player property inside an Entity however I'm receiving an undefined error.

This is how I&039;ve defined #player within main.js :

MyGame = ig.Game.extend({
  player: null,
  update: function() {
    this.player = this.getEntitiesByType( EntityPlayer )[0];
    if (this.player) {
      //etc
    }
  }
});

Then inside the Entity I'm calling:

ig.game.player.pos.x and I get ig.game.player is undefined

1 decade ago by dominic

From where exactly are you trying to access ig.game.player? From the init() of another entity? That init() is called before your first call to update(). Hence ig.game.player is still undefined.

Instead of getting the player entity for each frame in the update method, you could also just set it from within your player&039;s #init():
EntityPlayer = ig.Entity.extend({
	init: function( x, y, settings ) {
		this.parent( x, y, settings );
		ig.game.player = this;
	}
});

Make sure that the player entity is created before you try to access it.

If you want to access it from the init() of another entity, and you&039;re not sure if this other entity spawns before or after the player, use <2> instead of if #init().

1 decade ago by fulvio

Thanks dominic, it fixed the issue for me.
Page 1 of 1
« first « previous next › last »