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 Silent

Hello.

I want my NPCs to glow whenever they're in contact with the player. I need a flag that will tell me whether or not the NPC is touching the player, and set the animation accordingly.

I have the following code -
update: function () {
	this.parent();

	var player = MyGame.getEntitiesByType( EntityPlayer )[0];

	if(this.touches(player)) {
		// Glow
	}
}

However, apparently, MyGame.getEntitiesByType is not a function. I assume it has something to do with my NPC class not knowing MyGame. Can someone please give me a hint?


Thanks in advance.

1 decade ago by dominic

MyGame is the your game Class . getEntitiesByType() is a method of Instances of the Game Class. The currently running Game instance can always be accessed at ig.game (lowercase g):

var player = ig.game.getEntitiesByType( EntityPlayer )[0];

Btw.: getEntitiesByType() does a linear search through the entities array, which might be slow if you have a lot of entities in your level.

A solution I often use in my games is setting a reference to the player directly on the game instance. I.e. in your Player Entity:

init( x, y, settings ) {
    // ...
    ig.game.player = this;
}

Then you can access ig.game.player from anywhere in your code, without using getEntitiesByType()


Also keep in mind that touches() is only executed for overlapping entities. Judging from your other thread these entities may not be allowed to overlap at all. You can use collideWith() instead.

1 decade ago by Silent

Thanks a lot, that's exactly what I was looking for :)
Page 1 of 1
« first « previous next › last »