1 decade ago by fulvio
I have a few NPC's and I'd like to detect whether my player is standing to the left of them or to the right of them and flip the NPC so they're always facing the player.
So far I have this
If you need further code, please let me know. That update method is sitting inside the NPC entities.
So far I have this
update()
method, however it doesn't seem to be working quite as expected. Rather than flip the NPC whether my player is on the left or right it depends on how close I am to the NPC.update: function() { var xdist = Math.abs(ig.game.player.pos.x - this.pos.x); var xdir = ig.game.player.pos.x - this.pos.x < 0 ? -1 : 1; if (xdist >= 5 && xdir == -1) { this.currentAnim.flip.x = -1; } else { this.currentAnim.flip.x = 0; } this.parent(); }
If you need further code, please let me know. That update method is sitting inside the NPC entities.