1

I need to check if my player is on the left side or the right side of my enemy sprite so that I can move the enemy toward the player in that direction.

How do you check what side a sprite is on relative to another sprite?

I am using Kaboom.js, Javascript, on Replit.com


export default function run(speed = 250) {
    return {
        id: "run",
        require: [ "pos", "area", ],
        add() {
            if (   player is on left of enemy   ) {
                this.move(LEFT, speed)
            } else if (   player is on right of enemy   ) {
                this.move(RIGHT, speed)
            }
        },
    }
}

Ash Hanson
  • 23
  • 3
  • 2
    You need to compare the x coordinate of the enemy and player. if (player.x < enemy.x) moveleft. if (player.x > enemy.x) moveright. I'm not familiar with the libraries you're using, looking online you can use Kaboom pos() method https://kaboomjs.com/#pos to get the coordinates. – Stewart Jun 04 '22 at 09:54

0 Answers0