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 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 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.

1 decade ago by quidmonkey

this.currentAnim.flip.x = ig.game.player.pos.x < this.pos.x;

1 decade ago by fulvio

@quidmonkey: Thank you. That's the perfect one line of code.
Page 1 of 1
« first « previous next › last »