0

Im making a game where I need the enemy to spot the player once in a certain range, and then start chasing the player. I cant find anything anywhere at all that would help with how to make sprites chase each other

Im using Kaboom.js, javascript only (no html), on replit.com

Here is my code so far but it does not work because to my knowledge Kaboom.js does not have a way to get only the x position of a sprite

export default function run(speed = 250, dir = -1) {
    return {
        id: "run",
        require: [ "pos", "area", ],
        add() {
                if (player.pos > this.pos) {
                    dir = -dir
                    this.play("runRight")
                } else if (player.posx < this.posx) {
                    dir = -dir
                    this.play("runLeft")
                }
        },
        update() {
            this.move(speed * dir, 0)
        },
    }
}
Ash Hanson
  • 23
  • 3
  • Check the distance between your enemy and player, if the distance is <= the activation radius, then have the enemy move along the slope between the two points. Youll have to use a two point distance formula and figure the slope starting at the enemy. Take this slop and add the x/y to the enemy. Multiply that slope by some vector to set the speed – Spencer May Jun 04 '22 at 16:38
  • https://doc.gameenv.repl.co/guide is an example of a simple shooter game. It covers how to make an enemy move toward the player. – Stewart Jun 04 '22 at 17:16

0 Answers0