I’m trying to make a 4-directions game and I’m stuck on how to make the enemy patrols with animation( like a rectangle path).
I tried two ways to solve it, first is the path follower, but I have no idea how to add the animation when enemies are toward different directions.
path = this.add.path(150, 150);
path.lineTo(300, 150);
path.lineTo(300, 300);
path.lineTo(150, 300);
path.lineTo(150, 150);
enemies = this.add.follower(path, 0, 0, 'npc');
enemies.startFollow({
positionOnPath: true,
duration: 8000,
repeat: -1,
rotateToPath: false,
verticalAdjust: true
});
//I tried add animation here
enemy.anims.play('right');
the second one is time Event, I can make enemies randomly move with animation but I can’t figure out how to build a path.
In the function create(),
var timedEvent = this.time.addEvent({
delay: 500,
callback: moveEnemies,
callbackScope: this,
loop: true
});
,and this is the function I called in the time event
function moveEnemies () {
const randNumber = Math.floor((Math.random() * 4) + 1);switch(randNumber) { case 1: enemy.body.setVelocityX(50); enemy.anims.play('left'); repeat: 5; break; case 2: enemy.body.setVelocityX(-50); enemy.anims.play('left'); repeat: 5; break; case 3: enemy.body.setVelocityY(-50); enemy.anims.play('left'); repeat: 5; break; case 4: enemy.body.setVelocityY(50); enemy.anims.play('left'); repeat: 5; break; default: enemy.body.setVelocityX(50); } }
I also tried to set bound for the enemy but it doesn’t work,
if (enemy.x < 200) {
enemy.body.setVelocityX(0);
}
Any suggestions will be appreciated