Hello ! i have main scene:
export class Level1 extends Scene {
private player!: Player;
private enemies!: Phaser.GameObjects.Sprite[];
constructor() {
super("level-1-scene");
}
create(): void {
this.initMap();
this.player = new Player(this, 100, 100);
this.initEnemies();
}
update(): void {
this.player.update();
this.enemies.forEach((enm) => enm.update());
}
//...........................
private initEnemies(): void {
const enemyPoints = gameObjToObjPoint(
this.map.filterObjects("Enemies", (obj) => obj.name === "EnemyPoint")
);
this.enemies = enemyPoints.map((enemyPoint) =>
new Enemy(this, enemyPoint.x, enemyPoint.y, "tiles_spr", this.player, 503)
.setName(enemyPoint.id.toString())
.setScale(1.5)
);
this.physics.add.collider(this.enemies, this.wallsLayer);
this.physics.add.collider(this.enemies, this.enemies);
this.physics.add.collider(this.player, this.enemies, (obj1, obj2) => {
(obj1 as Player).getDamage(1);
});
}
}
Enemy class:
export class Enemy extends Actor {
private target: Player;
private AGRESSOR_RADIUS = 100;
private patrolPointX: number;
private patrolPointY: number;
private attackHandler: () => void;
constructor(
scene: Phaser.Scene,
x: number,
y: number,
texture: string,
target: Player,
frame?: string | number
) {
super(scene, x, y, texture, frame);
this.patrolPointX = this.x + 100;
this.patrolPointY = this.y + 100;
this.attackHandler = () => {
if (
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.target.x, y: this.target.y }
) < this.target.width
) {
this.getDamage();
this.disableBody(true, false);
this.scene.time.delayedCall(300, () => {
this.destroy();
});
}
};
this.target = target;
scene.add.existing(this);
scene.physics.add.existing(this);
this.getBody().setSize(16, 16);
this.getBody().setOffset(0, 0);
//....................
}
// i code some like this but its not working
update(): void {
this.getBody().setVelocityX(0);
if (
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.patrolPointX, y: this.y }
) <= 100 &&
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.patrolPointX, y: this.y }
) !== 0
) {
console.log(
"between enemy and patrol point",
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.patrolPointX, y: this.y }
) <= 100
);
this.body.velocity.x = 100;
} else if (
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.patrolPointX, y: this.y }
) === 0
) {
console.log(
"between enemy and patrol point zero",
Phaser.Math.Distance.BetweenPoints(
{ x: this.x, y: this.y },
{ x: this.patrolPointX, y: this.y }
) === 100
);
this.body.velocity.x = -100;
}
}
}
So how can make my enemy goig from left to right and from right to lef all time ?
i tried this approach (see comment in Enemy ) class